Nginx反向 Proxy以及配置最佳化

來源:互聯網
上載者:User

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

  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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.