nginx的使用配置

nginx为反向代理服务器,可以反向代理不同域名转向不同的具体服务器。可以用于负载压力或是同一台机器使用不同域名进行访问。

以下片段是服务器配置:

  1 #user cmcc;
  2 
  3 worker_processes 16;
  4 #worker_cpu_affinity 00000001 00000010 00000100 00001000;
  5 worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 00100000000000000000 0100000000000000 1000000000000000;
  6 #pid /var/run/nginx.pid;
  7 #error_log /var/log/nginx/error_log error;
  8 worker_rlimit_nofile 655350;
  9 events {
 10     use epoll;
 11     worker_connections 655350;
 12 }
 13 http {    
 14     include /etc/nginx/mime.types;
 15     default_type application/octet-stream;
 16     access_log /tmp/access.log;
 17     error_log /tmp/error.log;
 18     server_names_hash_bucket_size 128;
 19     client_header_buffer_size 4k;
 20     large_client_header_buffers 4 4k;
 21     client_max_body_size 10m;
 22     open_file_cache max=655350 inactive=20s;
 23     open_file_cache_min_uses 1;
 24     open_file_cache_valid 30s;
 25     sendfile on;
 26     gzip on;
 27     gzip_min_length 1k;
 28     gzip_buffers 4 16k;
 29     gzip_http_version 1.0;
 30     gzip_comp_level 2;
 31     gzip_types text/plain application/x-javascript text/cssapplication/xml;
 32     gzip_vary on;
 33 
 34     include /etc/nginx/conf.d/*.conf;
 35     include /etc/nginx/sites-enabled/*;
 36 
 37 #proxy server
 38     upstream webvoc.com{
 39         server 115.28.213.130:8080 weight=2;
 40     }
 41     upstream www.webvoc.com{
 42         server 115.28.213.130:8080 weight=2;
 43     }
 44     #upstream tomcat_stat {
 45     #    server 111.13.47.186:8080 weight=3;
 46     #}
 47 
 48 #visual master_server
 49 server {
 50         listen       8011;
 51         server_name  localhost;
 52 
 53         #charset koi8-r;
 54 
 55         #access_log  logs/host.access.log  main;
 56 
 57         location / {
 58             root   /usr/local/www;
 59             index  index.php;
 60         }
 61         
 62         location /svn { 
 63             proxy_set_header Host $host; 
 64             proxy_set_header X-Real-IP $remote_addr; 
 65             proxy_set_header X-Forwarded-Proto https; 
 66             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
 67             proxy_pass http://127.0.0.1:8099/svn/; 
 68         } 
 69         #error_page  404              /404.html;
 70 
 71         # redirect server error pages to the static page /50x.html
 72         #
 73         error_page   500 502 503 504  /50x.html;
 74         location = /50x.html {
 75             root   /usr/local/www;
 76         }
 77 
 78         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 79         #
 80         #location ~ \.php$ {
 81         #    proxy_pass   http://127.0.0.1;
 82         #}
 83 
 84         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 85         #
 86         location ~ \.php$ {
 87             root           /usr/local/www;
 88             fastcgi_pass   127.0.0.1:9000;
 89             fastcgi_index  index.php;
 90             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
 91             include        fastcgi_params;
 92         }
 93 
 94         # deny access to .htaccess files, if Apache's document root
 95         # concurs with nginx's one
 96         #
 97         #location ~ /\.ht {
 98         #    deny  all;
 99         #}
100     }
101     server{
102         listen 80;
103         server_name webvoc.com;
104         error_log /tmp/vm_err.log;
105         access_log /tmp/vm_access.log;
106         location /{
107             proxy_pass      http://webvoc.com;
108             client_max_body_size    10m;
109             client_body_buffer_size 128k;
110             proxy_read_timeout 120;
111             proxy_connect_timeout 120;
112             proxy_buffer_size 32k;
113             proxy_buffers 4 64k;
114             proxy_busy_buffers_size 128k;
115             proxy_headers_hash_bucket_size 128;
116             proxy_temp_file_write_size 128k;
117             proxy_set_header X-Real-IP $remote_addr;
118             proxy_set_header X-Forwarded-For $remote_addr;
119             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
120         }
121         location ~ .(qqLogin|registerQQ|registerWeiBo).action$ {
122             proxy_pass http://webvoc.com;   
123         }
124 
125         location ~ .(oauth2_qq).jsp$ {
126             proxy_pass   http://webvoc.com;
127         }    
128 
129         error_page   500 502 503 504  /50x.html;
130             location = /50x.html 
131                 {
132                         root   /usr/share/nginx/www;
133                 }
134 
135         #location /NginxStatus {
136 
137         #    stub_status on;
138         #    access_log off;
139         #    auth_basic "NginxStatus";
140         #}
141 
142         location ~* \.(gif|jpg|jpeg|questionStyle|kindeditor|fileupload)$ {
143     
144                  proxy_pass      http://webvoc.com;
145                 }    
146         
147     }    
148     server{
149         listen 80;
150         server_name www.webvoc.com;
151         error_log /tmp/vm_err.log;
152         access_log /tmp/vm_access.log;
153         location /{
154             proxy_pass      http://www.webvoc.com;
155             client_max_body_size    10m;
156             client_body_buffer_size 128k;
157             proxy_read_timeout 120;
158             proxy_connect_timeout 120;
159             proxy_buffer_size 32k;
160             proxy_buffers 4 64k;
161             proxy_busy_buffers_size 128k;
162             proxy_headers_hash_bucket_size 128;
163             proxy_temp_file_write_size 128k;
164             proxy_set_header X-Real-IP $remote_addr;
165             proxy_set_header X-Forwarded-For $remote_addr;
166             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
167         }
168         location ~ .(qqLogin|registerQQ|registerWeiBo).action$ {
169             proxy_pass http://webvoc.com;   
170         }
171 
172         location ~ .(oauth2_qq).jsp$ {
173             proxy_pass   http://www.webvoc.com;
174         }    
175 
176         error_page   500 502 503 504  /50x.html;
177             location = /50x.html 
178                 {
179                         root   /usr/share/nginx/www;
180                 }
181 
182         #location /NginxStatus {
183 
184         #    stub_status on;
185         #    access_log off;
186         #    auth_basic "NginxStatus";
187         #}
188 
189         location ~* \.(gif|jpg|jpeg|questionStyle|kindeditor|fileupload)$ {
190     
191                  proxy_pass      http://www.webvoc.com;
192                 }    
193         
194     }    
195 }

 

#proxy server 在这里配置一个代理名称,最重要的是server指向的地址。

如下:

upstream webvoc.com{
  server 115.28.213.130:8080 weight=2;
 }

名称为webvoc.com的域名指向了115.28.213.130:8080的实际地址。

weight=2向的反向代理权重,当多个负载服务器工作时,可以加大转向某个服务器的机率。

upstream www.webvoc.com{
  server 115.28.213.130:8080 weight=2;
 }

表明使用www.webvoc.com指向同一个网站。

 1     server{
 2         listen 80;
 3         server_name webvoc.com;
 4         error_log /tmp/vm_err.log;
 5         access_log /tmp/vm_access.log;
 6         location /{
 7             proxy_pass      http://webvoc.com;
 8             client_max_body_size    10m;
 9             client_body_buffer_size 128k;
10             proxy_read_timeout 120;
11             proxy_connect_timeout 120;
12             proxy_buffer_size 32k;
13             proxy_buffers 4 64k;
14             proxy_busy_buffers_size 128k;
15             proxy_headers_hash_bucket_size 128;
16             proxy_temp_file_write_size 128k;
17             proxy_set_header X-Real-IP $remote_addr;
18             proxy_set_header X-Forwarded-For $remote_addr;
19             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
20         }
21         location ~ .(qqLogin|registerQQ|registerWeiBo).action$ {
22             proxy_pass http://webvoc.com;   
23         }
24 
25         location ~ .(oauth2_qq).jsp$ {
26             proxy_pass   http://webvoc.com;
27         }    
28 
29         error_page   500 502 503 504  /50x.html;
30             location = /50x.html 
31                 {
32                         root   /usr/share/nginx/www;
33                 }
34 
35         #location /NginxStatus {
36 
37         #    stub_status on;
38         #    access_log off;
39         #    auth_basic "NginxStatus";
40         #}
41 
42         location ~* \.(gif|jpg|jpeg|questionStyle|kindeditor|fileupload)$ {
43     
44                  proxy_pass      http://webvoc.com;
45                 }    
46         
47     }    

server:是一个完整的服务器转向规则说明:

 listen:是要侦听的端口号,如果使用域名访问,这里可全部设置为80端口。

server_name:这个就是上面提到upstream后面的名字。一般情况下和域名相同,但是可以不一致。

这个nginx配置中,有一个站点为PHP站点,直接使用nginx作解析。配置代码如下:

 1 server {
 2         listen       8011;
 3         server_name  localhost;
 4 
 5         #charset koi8-r;
 6 
 7         #access_log  logs/host.access.log  main;
 8 
 9         location / {
10             root   /usr/local/www;
11             index  index.php;
12         }
13         
14         location /svn { 
15             proxy_set_header Host $host; 
16             proxy_set_header X-Real-IP $remote_addr; 
17             proxy_set_header X-Forwarded-Proto https; 
18             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
19             proxy_pass http://127.0.0.1:8099/svn/; 
20         } 
21         #error_page  404              /404.html;
22 
23         # redirect server error pages to the static page /50x.html
24         #
25         error_page   500 502 503 504  /50x.html;
26         location = /50x.html {
27             root   /usr/local/www;
28         }
29 
30         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
31         #
32         #location ~ \.php$ {
33         #    proxy_pass   http://127.0.0.1;
34         #}
35 
36         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
37         #
38         location ~ \.php$ {
39             root           /usr/local/www;
40             fastcgi_pass   127.0.0.1:9000;
41             fastcgi_index  index.php;
42             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
43             include        fastcgi_params;
44         }
45 
46         # deny access to .htaccess files, if Apache's document root
47         # concurs with nginx's one
48         #
49         #location ~ /\.ht {
50         #    deny  all;
51         #}
52     }

 

posted @ 2014-09-23 11:33  迟来的春天  阅读(745)  评论(0编辑  收藏  举报