Docker 安装

  1. # 1)安装依赖包
  2. yum install -y yum-utils device-mapper-persistent-data lvm2
  3.  
  4. # 2)添加Docker软件包源(否则doker安装的不是新版本)
  5. yum-config-manager \
  6. --add-repo \
  7. https://download.docker.com/linux/centos/docker-ce.repo
  8.  
  9. # 3)安装Docker CE
  10. yum install -y docker-ce
  11.  
  12. # 4)启动Docker服务并设置开机启动
  13. systemctl start docker
  14. systemctl enable docker
  15.  
  16. # 5)测试docker是否安装成功(hello-world是官方提供的一个测试镜像)
  17. docker run hello-world
  18.  
  19. # 6)查看docker基本信息
  20. docker info
  21. docker version
  22.  
  23. # 7)下载Nginx镜像
  24. docker run -itd nginx

docker简单使用(创建一个ngixn容器)

  1. # 1、创建一个nginx容器
  2. docker run -it nginx
  3.  
  4. # 2、查看docker运行的容器(可以获取到这个容器的id)
  5. docker ps
  6.  
  7. # 3、访问这个容器
  8. # 进入这个nginx容器(进入的文件系统和宿主机是完全隔离的,有自己独立的文件系统)
  9. docker exec -it 73877e65c07d bash
  10.  
  11. # 4、查看当前容器的 IP
  12. docker inspect 73877e65c07d # 73877e65c07d是通过docekr ps查看到的容器ID
  13. curl 172.17.0.2 # 测试这个nginx容器是否可以访问

Nginx负载均衡配置

  1. # 使用docker搭建第一台nginx服务
  2. [root@linux-node4 ~]# docker container run -d --name web01 -p 81:80 nginx
  3. 测试访问:http://192.168.56.14:81/ # 可以在浏览器输入自己的地址,访问到Nginx页面
  4. root@c58a7f1fb89d:/# docker exec -it web01 bash
  5. root@c58a7f1fb89d:/# echo web01 > /usr/share/nginx/html/index.html
  1. # 使用docker搭建第二台nginx服务
  2. [root@linux-node4 ~]# docker container run -d --name web02 -p 82:80 nginx
  3. 测试访问:http://192.168.56.14:82/ # 可以在浏览器输入自己的地址,访问到Nginx页面root@a3440d30f27c:/# docker exec -it web02 bash
  4. root@a3440d30f27c:/# echo web02 > /usr/share/nginx/html/index.html

默认轮训(在真实主机中安装nginx并配置负载均衡)

  1. [root@izbp19kniw9k2ljqdjmld5z ~]# cd /etc/nginx/conf.d
  2. [root@izbp19kniw9k2ljqdjmld5z conf.d]# vim default.conf

  1. # upstream是自己写的,一定要放在server外面,切记端口号是自己的
  2. upstream myservers {
  3. server 192.168.56.14:81;
  4. server 192.168.56.14:82;
  5. }
  6.  
  7. # server其实默认已经有一个,只需要修改location中配置,指定转发代理即可
  8. server {
  9. location / {
  10. proxy_pass http://myservers;
  11. }
  12. }
  13. # 配置好以后保存执行下面这俩条命令即可:
  14. # 重启:systemctl restart nginx
  15. # 开启 systemctl start nginx
  16.  
  17. # 简单的配置就OK了!

Nginx 配置文件注释

  1. #运行用户
  2.  
  3. user nobody;
  4.  
  5. #启动进程,通常设置成和cpu的数量相等
  6.  
  7. worker_processes 1;
  8.  
  9. #全局错误日志及PID文件
  10.  
  11. #error_log logs/error.log;
  12.  
  13. #error_log logs/error.log notice;
  14.  
  15. #error_log logs/error.log info;
  16.  
  17. #pid logs/nginx.pid;
  18.  
  19. #工作模式及连接数上限
  20.  
  21. events {
  22.  
  23. #epoll是多路复用IO(I/O Multiplexing)中的一种方式,
  24.  
  25. #仅用于linux2.6以上内核,可以大大提高nginx的性能
  26.  
  27. use epoll;
  28.  
  29. #单个后台worker process进程的最大并发链接数
  30.  
  31. worker_connections 1024;
  32.  
  33. # 并发总数是 worker_processes 和 worker_connections 的乘积
  34.  
  35. # 即 max_clients = worker_processes * worker_connections
  36.  
  37. # 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4 为什么
  38.  
  39. # 为什么上面反向代理要除以4,应该说是一个经验值
  40.  
  41. # 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000
  42.  
  43. # worker_connections 值的设置跟物理内存大小有关
  44.  
  45. # 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数
  46.  
  47. # 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右
  48.  
  49. # 我们来看看360M内存的VPS可以打开的文件句柄数是多少:
  50.  
  51. # $ cat /proc/sys/fs/file-max
  52.  
  53. # 输出 34336
  54.  
  55. # 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内
  56.  
  57. # 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置
  58.  
  59. # 使得并发总数小于操作系统可以打开的最大文件数目
  60.  
  61. # 其实质也就是根据主机的物理CPU和内存进行配置
  62.  
  63. # 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。
  64.  
  65. # ulimit -SHn 65535
  66.  
  67. }
  68.  
  69. http {
  70.  
  71. #设定mime类型,类型由mime.type文件定义
  72.  
  73. include mime.types;
  74.  
  75. default_type application/octet-stream;
  76.  
  77. #设定日志格式
  78.  
  79. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  80.  
  81. '$status $body_bytes_sent "$http_referer" '
  82.  
  83. '"$http_user_agent" "$http_x_forwarded_for"';
  84.  
  85. access_log logs/access.log main;
  86.  
  87. #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
  88.  
  89. #对于普通应用,必须设为 on,
  90.  
  91. #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
  92.  
  93. #以平衡磁盘与网络I/O处理速度,降低系统的uptime.
  94.  
  95. sendfile on;
  96.  
  97. #tcp_nopush on;
  98.  
  99. #连接超时时间
  100.  
  101. #keepalive_timeout 0;
  102.  
  103. keepalive_timeout 65;
  104.  
  105. tcp_nodelay on;
  106.  
  107. #开启gzip压缩
  108.  
  109. gzip on;
  110.  
  111. gzip_disable "MSIE [1-6].";
  112.  
  113. #设定请求缓冲
  114.  
  115. client_header_buffer_size 128k;
  116.  
  117. large_client_header_buffers 4 128k;
  118.  
  119. #设定虚拟主机配置
  120.  
  121. server {
  122.  
  123. #侦听80端口
  124.  
  125. listen 80;
  126.  
  127. #定义使用 www.nginx.cn访问
  128.  
  129. server_name www.nginx.cn;
  130.  
  131. #定义服务器的默认网站根目录位置
  132.  
  133. root html;
  134.  
  135. #设定本虚拟主机的访问日志
  136.  
  137. access_log logs/nginx.access.log main;
  138.  
  139. #默认请求
  140.  
  141. location / {
  142.  
  143. #定义首页索引文件的名称
  144.  
  145. index index.php index.html index.htm;
  146.  
  147. }
  148.  
  149. # 定义错误提示页面
  150.  
  151. error_page 500 502 503 504 /50x.html;
  152.  
  153. location = /50x.html {
  154.  
  155. }
  156.  
  157. #静态文件,nginx自己处理
  158.  
  159. location ~ ^/(images|javascript|js|css|flash|media|static)/ {
  160.  
  161. #过期30天,静态文件不怎么更新,过期可以设大一点,
  162.  
  163. #如果频繁更新,则可以设置得小一点。
  164.  
  165. expires 30d;
  166.  
  167. }
  168.  
  169. #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
  170.  
  171. location ~ .php$ {
  172.  
  173. fastcgi_pass 127.0.0.1:9000;
  174.  
  175. fastcgi_index index.php;
  176.  
  177. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  178.  
  179. include fastcgi_params;
  180.  
  181. }
  182.  
  183. #禁止访问 .htxxx 文件
  184.  
  185. location ~ /.ht {
  186.  
  187. deny all;
  188.  
  189. }
  190.  
  191. }
  192.  
  193. }

Nginx配置举例

  1. user work;
  2.  
  3. worker_processes 8;
  4.  
  5. worker_rlimit_nofile 65535;
  6.  
  7. error_log logs/error.log warn;
  8.  
  9. #error_log logs/error.log notice;
  10.  
  11. #error_log logs/error.log info;
  12.  
  13. pid logs/nginx.pid;
  14.  
  15. events {
  16.  
  17. use epoll;
  18.  
  19. worker_connections 65535;
  20.  
  21. }
  22.  
  23. # load modules compiled as Dynamic Shared Object (DSO)
  24.  
  25. #
  26.  
  27. #dso {
  28.  
  29. # load ngx_http_fastcgi_module.so;
  30.  
  31. # load ngx_http_rewrite_module.so;
  32.  
  33. #}
  34.  
  35. http {
  36.  
  37. include mime.types;
  38.  
  39. default_type application/octet-stream;
  40.  
  41. server_names_hash_bucket_size 128;
  42.  
  43. sendfile on;
  44.  
  45. tcp_nopush on;
  46.  
  47. tcp_nodelay on;
  48.  
  49. fastcgi_connect_timeout 5;
  50.  
  51. fastcgi_send_timeout 10;
  52.  
  53. fastcgi_read_timeout 10;
  54.  
  55. fastcgi_buffer_size 64k;
  56.  
  57. fastcgi_buffers 4 64k;
  58.  
  59. fastcgi_busy_buffers_size 128k;
  60.  
  61. fastcgi_temp_file_write_size 128k;
  62.  
  63. #keepalive_timeout 0;
  64.  
  65. keepalive_timeout 60;
  66.  
  67. keepalive_requests 1024;
  68.  
  69. client_header_buffer_size 4k;
  70.  
  71. large_client_header_buffers 4 32k;
  72.  
  73. client_max_body_size 10m;
  74.  
  75. client_body_buffer_size 512k;
  76.  
  77. client_body_timeout 600;
  78.  
  79. client_header_timeout 600;
  80.  
  81. send_timeout 600;
  82.  
  83. proxy_connect_timeout 1000ms;
  84.  
  85. proxy_send_timeout 2000000ms;
  86.  
  87. proxy_read_timeout 2000000ms;
  88.  
  89. proxy_buffers 64 8k;
  90.  
  91. proxy_busy_buffers_size 128k;
  92.  
  93. proxy_temp_file_write_size 64k;
  94.  
  95. proxy_redirect off;
  96.  
  97. #proxy_next_upstream off ;
  98.  
  99. gzip on;
  100.  
  101. gzip_min_length 1k;
  102.  
  103. gzip_buffers 4 16k;
  104.  
  105. gzip_http_version 1.0;
  106.  
  107. gzip_comp_level 2;
  108.  
  109. gzip_types text/plain application/x-javascript text/css application/xml;
  110.  
  111. gzip_vary on;
  112.  
  113. add_header X-Frame-Options "ALLOW-FROM http://cloud.njsig.cn";
  114.  
  115. proxy_set_header X-Real-IP $remote_addr;
  116.  
  117. proxy_set_header X-Real-Port $remote_port;
  118.  
  119. proxy_set_header Host $host;
  120.  
  121. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  122.  
  123. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  124.  
  125. '$status $body_bytes_sent "$http_referer" "$http_cookie" "$http_user_agent" '
  126.  
  127. '$request_time $remote_addr $server_addr $upstream_addr $host '
  128.  
  129. '"$http_x_forwarded_for" $upstream_response_time';
  130.  
  131. set_real_ip_from 10.0.0.0/8;
  132.  
  133. real_ip_header X-Real-IP;
  134.  
  135. #example
  136.  
  137. # server {
  138.  
  139. # listen 8000;
  140.  
  141. # server_name www;
  142.  
  143. # access_log logs/access.log main;
  144.  
  145. # location / {
  146.  
  147. # proxy_pass http://127.0.0.1:8001;
  148.  
  149. # }
  150.  
  151. #
  152.  
  153. #
  154.  
  155. # }
  156.  
  157. include vhosts/*.conf;
  158.  
  159. }

nginx/conf/vhosts/opwf.conf django项目简单配置

  1. server {
  2.  
  3. listen 80;
  4.  
  5. server_name aaa.test.com bbb.test.com;
  6.  
  7. access_log /home/work/nginx/logs/opwf_access.log main;
  8.  
  9. location / {
  10.  
  11. proxy_pass http://127.0.0.1:8001;
  12.  
  13. }
  14. }

nginx/conf/vhosts/opwf.conf django项目简单配置

  1. server {
  2.  
  3. listen 80;
  4.  
  5. server_name ccc.test.com;
  6.  
  7. access_log /home/work/nginx/logs/nj1_access.log main;
  8.  
  9. root /home/work/project/frontopwf/dist;
  10.  
  11. location / {
  12.  
  13. try_files $uri $uri/ @router;
  14. }
  15.  
  16. location @router {
  17.  
  18. rewrite ^.*$ /index.html last;
  19. }
  20. }
  1. # 可以在浏览器输入自己的地址,访问到Nginx页面

Docker 安装 Nginx 负载均衡配置的更多相关文章

  1. Docker容器Nginx负载均衡配置、check及stub模块安装

    Nginx是一款高性能的HTTP和反向代理.负载均衡web服务器.本次在Docker容器中部署三个tomcat,Nginx代理三个tomcat服务(以下称节点)来模拟实现负载均衡效果,配置check模 ...

  2. nginx安装及负载均衡配置

    Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev 为俄罗斯访问量第二 ...

  3. Nginx负载均衡配置简单配置方法

    http://www.jb51.net/article/121235.htm Nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至不同的Web服务器.下面通过 ...

  4. nginx高性能WEB服务器系列之六--nginx负载均衡配置+健康检查

    nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...

  5. Mall电商项目总结(二)——nginx负载均衡配置和策略

    1. nginx配置文件 用户在浏览器上输入,http://www.xwld.site/ 实际上是在访问服务器80端口,nginx 监听80端口,将用户的请求转发到8080和9080端口 . upst ...

  6. Tomcat服务部署与Nginx负载均衡配置

    一.中间键产品介绍 目前来说IBM的WebSphere,Oracle的Weblogic占据了市场上java语言Web站点的部分份额,该两种软件都是商业化的软件,由于性能优越,可靠性高等优点应用于大型互 ...

  7. Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群

    Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群 >>>>>>>>>>>> ...

  8. Nginx负载均衡配置实例详解

    负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...

  9. Nginx负载均衡配置实例详解(转)

    负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...

随机推荐

  1. Cf D. Nauuo and Circle

    https://codeforces.com/contest/1173/problem/D 题意: 给出你一个包含 n 个点的树,这 n 个点编号为 1~n: 给出一个圆,圆上放置 n 个位置,第 i ...

  2. 编译安装 logstash-output-jdbc

    环境 mac https://github.com/theangryangel/logstash-output-jdbc logstash-plugin install logstash-output ...

  3. 电影画面赏析_唐顿庄园S01E01

    唐顿庄园S01E01 1. 2. 3. 4. 5. 6. 7. 8.

  4. @EnableGlobalMethodSecurity(prePostEnabled = true)

    http://www.bubuko.com/infodetail-2243816.html

  5. django框架进阶-admin-长期维护

    ###############    django--admin的使用    ################ # django后台管理: # 第一步: # 在settings文件中修改语言和时区 L ...

  6. 吴裕雄--天生自然python学习笔记:python爬虫与网页分析

    我们所抓取的网页源代码一般都是 HTML 格式的文件,只要研究明白 HTML 中 的标签( Tag )结构,就很容易进行解析并取得所需数据 . HTML 网页结构 HTML 网 页是由许多标签( Ta ...

  7. js寄生组合式继承

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Ionic3学习笔记(十)实现夜间模式功能

    本文为原创文章,转载请标明出处 目录 创建主题样式 导入 variables.scss 创建 provider 创建 page 在 App 入口处应用主题 效果图 1. 创建主题样式 在 ./src/ ...

  9. 吴裕雄--天生自然 python数据分析:加纳卫生设施数据分析

    import numpy as np # linear algebra import pandas as pd # data processing, CSV file I/O (e.g. pd.rea ...

  10. LLDB奇巧淫技

    打印视图层级 这个相信很多人都会了,是ta是ta就是ta recursiveDescription 用法大概就是如下 123 po [self.view recursiveDescription] p ...