Nginx反向代理以及配置优化

来源:互联网 发布:哈利波特宝箱淘宝 编辑:程序博客网 时间:2024/05/20 16:11

下面配置包含了,nginx配置的一个比较全面的反向代理的例子:

[plain] view plaincopy
  1. user www www  
  2. worker_processes  8;  
  3.   
  4.   
  5.   
  6. events {  
  7.     use epoll;  
  8.     worker_connections  10240;  
  9. }  
  10.   
  11. pid       /opt/work/log/nginx.pid;  
  12. error_log /dev/null error;  
  13.   
  14. http {  
  15.   
  16.     include /opt/nginx/conf/mime.types;  
  17.     default_type     text/html;  
  18.   
  19.      log_format  pv  '[$time_local]|$remote_addr|$cookie_SUV|$args|$http_referer|$http_user_agent';  
  20.   
  21.      log_format   main '$request_time $remote_addr $cookie_SUV [$time_local]  $status '  '"$request" $body_bytes_sent "$http_referer" '  '"$http_user_agent" "$http_x_forwarded_for"'  '"$cookie_Fr"';  
  22.       
  23.      log_format  pbmain    '$remote_addr - $remote_user [$time_local] "$request" '  
  24.                                 '$status $body_bytes_sent "$http_user_agent"';  
  25.      access_log off;  
  26.   
  27.      sendfile    on;  
  28.   
  29.   
  30.      keepalive_timeout  0;  
  31.   
  32.      gzip  on;  
  33.       
  34.     #允许客户端请求的最大单个文件字节数  
  35.      client_max_body_size    200m;  
  36.     upload_progress proxied 8m;  
  37.       
  38.     #DNS服务器ip  
  39.     resolver 192.168.1.1;  
  40.       
  41.     proxy_set_header Host $http_host;  
  42.     proxy_set_header x-forwarded-for  $remote_addr;  
  43.     proxy_cache_key  "$scheme$host$request_uri";  
  44.     proxy_cache_path /opt/work/cache levels=1:2:1 keys_zone=js_cache:100m max_size=1024m inactive=15d;  
  45.     proxy_cache_valid any 15d;  
  46.   
  47.     upstream backend {  
  48.         ip_hash;  
  49.         server  127.0.0.1:8080 weigth=4 max_fails=2 fail_timeout=30s;  
  50.         server  127.0.0.1:8081 weigth=3 max_fails=2 fail_timeout=30s;  
  51.     }  
  52.     upstream liveBackends {  
  53.        server   192.168.1.15:9090;  
  54.        server   192.168.1.16:9090;  
  55.        #备机  
  56.        server   192.168.1.17:9090 backup;  
  57.      }  
  58.   
  59.     server{  
  60.         listen 80;  
  61.         server_name www.myserver1.com;  
  62.         error_log   /opt/work/log/nginx_error.log error;  
  63.         client_max_body_size    200m;  
  64.         proxy_set_header Host $http_host;  
  65.         proxy_set_header x-forwarded-for  $remote_addr;  
  66.         proxy_buffer_size         64k;  
  67.         proxy_buffers             32 64k;  
  68.       
  69.         #查看服务状态  
  70.         location /server-status {  
  71.                stub_status on;  
  72.                access_log   off;  
  73.                allow all;  
  74.         }  
  75.         location ^~ /pv        {  
  76.             access_log  /opt/work/log1/nginx_access.log  pv;  
  77.             proxy_pass http://backend;  
  78.         }  
  79.     
  80.         location ^~ /cc.do  
  81.         {  
  82.             proxy_pass http://backend;  
  83.         }  
  84.         location ^~/favicon.ico{  
  85.             alias /usr/local/src/web/favicon.ico;  
  86.          }  
  87.   
  88.        location ^~ /pv2        {  
  89.             access_log  /opt/work/log2/nginx_access.log  pv;  
  90.         access_log  /opt/work/log/main/nginx_access_main.log main;  
  91.             error_log   /opt/work/log/error/nginx_error.log error;  
  92.             return 204;  
  93.         }  
  94.       
  95.        location /proxy/  
  96.         {  
  97.         #后端返回502,504,执行超时等错误,自动将请求转发到upstream负载池中另一台服务器  
  98.             proxy_next_upstream  http_502 http_504 error timeout invalid_header;  
  99.         proxy_pass http://backend;    
  100.         }  
  101.         #禁止访问除以外的其他目录  
  102.         location /{  
  103.            deny all;  
  104.         }  
  105.     }  
  106.   
  107.      server{  
  108.         listen       80;  
  109.         server_name  www.myserver2.com;  
  110.         root         /www;   
  111.     location ~ t1\.(gif)$ {  
  112.         root /www;  
  113.         expires -1;  
  114.                 access_log      /data/log/access.log  pbmain;  
  115.                 proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;  
  116.               
  117.     }  
  118.   
  119.         location ~ t2.(gif)$ {  
  120.                 root /www;  
  121.                 expires -1;               
  122.                 access_log      /data/log/access.log  pbmain;  
  123.                 proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;  
  124.              if ($args ~ "ltype=1(&|$)") {                                
  125.         proxy_pass http://liveBackends;  
  126.          }  
  127.         }  
  128.   
  129.          
  130.     location /server-status {  
  131.                         stub_status     on;  
  132.                         access_log      off;  
  133.                         allow           10.0.0.0/8;  
  134.                         allow           192.168.0.0/16;  
  135.                         deny            all;  
  136.          }  
  137.         error_page  404              /404.html;  
  138.   
  139.         error_page   500 502 503 504  /50x.html;  
  140.         location = /50x.html {  
  141.             root   html;  
  142.         }  
  143.     }  
  144.   
  145. }  

配置完后通过命令 nginx -t  (或者nginx -t -c 配置文件路径)来检查配置文件是否有语法错误,没有错误后就可以启动了 /usr/local/nginx/sbin/nginx -c  /usr/local/nginx/conf/nginx.conf ,用默认路径配置文件的话可以不加-c

从容停止kill -QUIT nginx主进程号

平滑重启 kill -HUP nginx主进程号

配置文件修改可以通过不停机reload的方式实现加载。 nginx -s reload