nginx的使用配置

时间:2023-03-08 19:58:21

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

以下片段是服务器配置:

 #user cmcc;

 worker_processes 16;
#worker_cpu_affinity 00000001 00000010 00000100 00001000;
worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 00100000000000000000 0100000000000000 1000000000000000;
#pid /var/run/nginx.pid;
#error_log /var/log/nginx/error_log error;
worker_rlimit_nofile 655350;
events {
use epoll;
worker_connections 655350;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /tmp/access.log;
error_log /tmp/error.log;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 4k;
client_max_body_size 10m;
open_file_cache max=655350 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
sendfile on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/cssapplication/xml;
gzip_vary on; include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*; #proxy server
upstream webvoc.com{
server 115.28.213.130:8080 weight=2;
}
upstream www.webvoc.com{
server 115.28.213.130:8080 weight=2;
}
#upstream tomcat_stat {
# server 111.13.47.186:8080 weight=3;
#} #visual master_server
server {
listen 8011;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root /usr/local/www;
index index.php;
} location /svn {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8099/svn/;
}
#error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/www;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/local/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server{
listen 80;
server_name webvoc.com;
error_log /tmp/vm_err.log;
access_log /tmp/vm_access.log;
location /{
proxy_pass http://webvoc.com;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_read_timeout 120;
proxy_connect_timeout 120;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_headers_hash_bucket_size 128;
proxy_temp_file_write_size 128k;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .(qqLogin|registerQQ|registerWeiBo).action$ {
proxy_pass http://webvoc.com;
} location ~ .(oauth2_qq).jsp$ {
proxy_pass http://webvoc.com;
} error_page 500 502 503 504 /50x.html;
location = /50x.html
{
root /usr/share/nginx/www;
} #location /NginxStatus { # stub_status on;
# access_log off;
# auth_basic "NginxStatus";
#} location ~* \.(gif|jpg|jpeg|questionStyle|kindeditor|fileupload)$ { proxy_pass http://webvoc.com;
} }
server{
listen 80;
server_name www.webvoc.com;
error_log /tmp/vm_err.log;
access_log /tmp/vm_access.log;
location /{
proxy_pass http://www.webvoc.com;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_read_timeout 120;
proxy_connect_timeout 120;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_headers_hash_bucket_size 128;
proxy_temp_file_write_size 128k;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .(qqLogin|registerQQ|registerWeiBo).action$ {
proxy_pass http://webvoc.com;
} location ~ .(oauth2_qq).jsp$ {
proxy_pass http://www.webvoc.com;
} error_page 500 502 503 504 /50x.html;
location = /50x.html
{
root /usr/share/nginx/www;
} #location /NginxStatus { # stub_status on;
# access_log off;
# auth_basic "NginxStatus";
#} location ~* \.(gif|jpg|jpeg|questionStyle|kindeditor|fileupload)$ { proxy_pass http://www.webvoc.com;
} }
}

#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指向同一个网站。

     server{
listen 80;
server_name webvoc.com;
error_log /tmp/vm_err.log;
access_log /tmp/vm_access.log;
location /{
proxy_pass http://webvoc.com;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_read_timeout 120;
proxy_connect_timeout 120;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_headers_hash_bucket_size 128;
proxy_temp_file_write_size 128k;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .(qqLogin|registerQQ|registerWeiBo).action$ {
proxy_pass http://webvoc.com;
} location ~ .(oauth2_qq).jsp$ {
proxy_pass http://webvoc.com;
} error_page 500 502 503 504 /50x.html;
location = /50x.html
{
root /usr/share/nginx/www;
} #location /NginxStatus { # stub_status on;
# access_log off;
# auth_basic "NginxStatus";
#} location ~* \.(gif|jpg|jpeg|questionStyle|kindeditor|fileupload)$ { proxy_pass http://webvoc.com;
} }

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

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

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

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

 server {
listen 8011;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root /usr/local/www;
index index.php;
} location /svn {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8099/svn/;
}
#error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/www;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/local/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}