萬盛學電腦網

 萬盛學電腦網 >> 服務器教程 >> Nginx反向代理以及配置優化

Nginx反向代理以及配置優化

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

  user www www

  worker_processes 8;

  events {

  use epoll;

  worker_connections 10240;

  }

  pid /opt/work/log/nginx.pid;

  error_log /dev/null error;

  http {

  include /opt/nginx/conf/mime.types;

  default_type text/html;

  log_format pv '[$time_local]|$remote_addr|$cookie_SUV|$args|$http_referer|$http_user_agent';

  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"';

  log_format pbmain '$remote_addr - $remote_user [$time_local] "$request" '

  '$status $body_bytes_sent "$http_user_agent"';

  access_log off;

  sendfile on;

  keepalive_timeout 0;

  gzip on;

  #允許客戶端請求的最大單個文件字節數

  client_max_body_size 200m;

  upload_progress proxied 8m;

  #DNS服務器ip

  resolver 192.168.1.1;

  proxy_set_header Host $http_host;

  proxy_set_header x-forwarded-for $remote_addr;

  proxy_cache_key "$scheme$host$request_uri";

  proxy_cache_path /opt/work/cache levels=1:2:1 keys_zone=js_cache:100m max_size=1024m inactive=15d;

  proxy_cache_valid any 15d;

  upstream backend {

  ip_hash;

  server 127.0.0.1:8080 weigth=4 max_fails=2 fail_timeout=30s;

  server 127.0.0.1:8081 weigth=3 max_fails=2 fail_timeout=30s;

  }

  upstream liveBackends {

  server 192.168.1.15:9090;

  server 192.168.1.16:9090;

  #備機

  server 192.168.1.17:9090 backup;

  }

  server{

  listen 80;

  server_name www.myserver1.com;

  error_log /opt/work/log/nginx_error.log error;

  client_max_body_size 200m;

  proxy_set_header Host $http_host;

  proxy_set_header x-forwarded-for $remote_addr;

  proxy_buffer_size 64k;

  proxy_buffers 32 64k;

  #查看服務狀態

  location /server-status {

  stub_status on;

  access_log off;

  allow all;

  }

  location ^~ /pv {

  access_log /opt/work/log1/nginx_access.log pv;

  proxy_pass http://backend;

  }

  location ^~ /cc.do

  {

  proxy_pass http://backend;

  }

  location ^~/favicon.ico{

  alias /usr/local/src/web/favicon.ico;

  }

  location ^~ /pv2 {

  access_log /opt/work/log2/nginx_access.log pv;

  access_log /opt/work/log/main/nginx_access_main.log main;

  error_log /opt/work/log/error/nginx_error.log error;

  return 204;

  }

  location /proxy/

  {

  #後端返回502,504,執行超時等錯誤,自動將請求轉發到upstream負載池中另一台服務器

  proxy_next_upstream http_502 http_504 error timeout invalid_header;

  proxy_pass http://backend;

  }

  #禁止訪問除以外的其他目錄

  location /{

  deny all;

  }

  }

  server{

  listen 80;

  server_name www.myserver2.com;

  root /www;

  location ~ t1.(gif)$ {

  root /www;

  expires -1;

  access_log /data/log/access.log pbmain;

  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  }

  location ~ t2.(gif)$ {

  root /www;

  expires -1;

  access_log /data/log/access.log pbmain;

  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  if ($args ~ "ltype=1(&|$)") {

  proxy_pass http://liveBackends;

  }

  }

  location /server-status {

  stub_status on;

  access_log off;

  allow 10.0.0.0/8;

  allow 192.168.0.0/16;

  deny all;

  }

  error_page 404 /404.html;

  error_page 500 502 503 504 /50x.html;

  location = /50x.html {

  root html;

  }

  }

  }

  配置完後通過命令 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

copyright © 萬盛學電腦網 all rights reserved