前言 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
可以在大多数 UnixLinux OS 上编译运行,并有 Windows 移植版。是一个很强大的高性能Web和反向代理服务,它具有很多非常优越的特性,在连接高并发的情况下,Nginx是Apache服务不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一,能够支持高达 50,000 个并发连接数的响应。
Nginx作为负载均衡服务:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP 代理服务 对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。那对于Nginx的相应配置,当集群出现某些问题的时候,参数会起到相当重要的作用,但是大家也知道,包括官网在内,很多的参数配置都是用英文进行讲解,今天就给大家整理一些配置中文详解
Nginx配置参数中文详细说明: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 user www www; worker_processes 8 ; error_log /var/log/nginx/error.log info; pid /var/run/nginx.pid; worker_rlimit_nofile 65535 ; events { use epoll; worker_connections 65535 ; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128 ; client_header_buffer_size 32 k; large_client_header_buffers 4 64 k; client_max_body_size 8 m; autoindex on; autoindex_exact_size on; autoindex_localtime on; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 120 ; fastcgi_connect_timeout 300 ; fastcgi_send_timeout 300 ; fastcgi_read_timeout 300 ; fastcgi_buffer_size 64 k; fastcgi_buffers 4 64 k; fastcgi_busy_buffers_size 128 k; fastcgi_temp_file_write_size 128 k; gzip on; gzip_min_length 1 k; gzip_buffers 4 16 k; gzip_http_version 1.1 ; gzip_comp_level 2 ; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on;#选项可以让前端的缓存服务器缓存经过gzip压缩的页面.例如:用squid缓存经过nginx压缩的数据 server { listen 80 ; server_name ably.com; rewrite ^(.*) https://$server_name$1 permanent; } server { listen 443 ssl; server_name ably.com; ssl_certificate C:\WebServer\Certs\certificate.crt; ssl_certificate_key C:\WebServer\Certs\private.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5 m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on; index index.html index.htm index.php; root /data/www/; location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0 .0 .1 :9000; fastcgi_index index.php; include fastcgi.conf; } location /oauth/{ proxy_pass https://localhost:13580/oauth/; proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 10 d; } location ~ .*\.(js|css)?$ { expires 1 h; } log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for' ; access_log /var/log/nginx/access.log access; location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus" ; auth_basic_user_file conf/htpasswd; } } }
Nginx多台服务器实现负载均衡: 1.Nginx负载均衡服务器: IP:192.168.0.4(Nginx-Server)
2.Web服务器列表: Web1:192.168.0.5(Nginx-Node1/Nginx-Web1) ;Web2:192.168.0.7(Nginx-Node2/Nginx-Web2)
3.实现目的:用户访问Nginx-Server(mongo.demo.com:8888)时,通过Nginx负载均衡到Web1和Web2服务器
Nginx负载均衡服务器的nginx.conf配置注释如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 events { use epoll; worker_connections 65535 ; } http { upstream webhost { server 192.168 .0 .5 :6666 ; server 192.168 .0 .7 :6666 ; } upstream webhost { server 192.168 .0 .5 :6666 weight=2; server 192.168 .0 .7 :6666 weight=3; } upstream webhost { ip_hash; server 192.168 .0 .5 :6666 ; server 192.168 .0 .7 :6666 ; } upstream webhost { server 192.168 .0 .5 :6666 ; server 192.168 .0 .7 :6666 ; hash $request_uri; } server { listen 80 ; server_name mongo.demo.com; location / { proxy_pass http://webhost; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; client_max_body_size 10 m; client_body_buffer_size 128 k; proxy_connect_timeout 90 ; proxy_send_timeout 90 ; proxy_read_timeout 90 ; proxy_buffer_size 4 k; proxy_buffers 4 32 k; proxy_busy_buffers_size 64 k; proxy_temp_file_write_size 64 k; } } }
负载均衡操作演示如下:
操作对象:192.168.0.4(Nginx-Server)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 $ mkdir -p /opt/confs $ vim /opt/confs/nginx.conf events { use epoll; worker_connections 65535 ; } http { upstream webhost { ip_hash; server 192.168 .0 .5 :6666 ; server 192.168 .0 .7 :6666 ; } server { listen 80 ; server_name mongo.demo.com; location / { proxy_pass http://webhost; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; client_max_body_size 10 m; client_body_buffer_size 128 k; proxy_connect_timeout 90 ; proxy_send_timeout 90 ; proxy_read_timeout 90 ; proxy_buffer_size 4 k; proxy_buffers 4 32 k; proxy_busy_buffers_size 64 k; proxy_temp_file_write_size 64 k; } } } docker run -d -p 8888 :80 --name nginx-server -v /opt/confs/nginx.conf:/etc/nginx/nginx.conf --restart always nginx
操作对象:192.168.0.5(Nginx-Node1/Nginx-Web1)
1 2 3 4 5 6 7 8 9 10 11 12 $ mkdir -p /opt/html $ vim /opt/html/index.html <div> <h1> The host is 192.168 .0 .5 (Docker02) - Node 1 ! </h1> </div> $ docker run -d -p 6666 :80 --name nginx-node1 -v /opt/html:/usr/share/nginx/html --restart always nginx
操作对象:192.168.0.7(Nginx-Node2/Nginx-Web2)
1 2 3 4 5 6 7 8 9 10 11 12 $ mkdir -p /opt/html $ vim /opt/html/index.html <div> <h1> The host is 192.168 .0 .7 (Docker03) - Node 2 ! </h1> </div> $ docker run -d -p 6666 :80 --name nginx-node2 -v $(pwd)/html:/usr/share/nginx/html --restart always nginx