nginx怎样做反向代理服务器

废话不多说,直接贴配置文件

  server {
     listen       80;
     server_name   b.ildd.cc;
  
     #charset koi8-r;
     #access_log  /usr/local/nginx/logs/host.access.log  main;
     if (!-e $request_filename){
             rewrite ^/(.*) /index.php last;  
     }  
  
     location / {
       proxy_pass http://172.17.0.3:80;
         index  index.php index.html index.htm;
     }
  
     #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 {
     }
  
     # 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$ {
         fastcgi_index   index.php;
         fastcgi_pass    127.0.0.1:9000;
         include           fastcgi_params;
         fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
         fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
     }
  
     # deny access to .htaccess files, if Apache's document root
     # concurs with nginx's one
     #
     #location ~ /\.ht {
     #    deny  all;
     #}
 } 

重点内容在加黑的位置,这只是简单的一些设置。访问b.ildd.cc自动跳转到172.17.0.3.

如果需要做负载均衡的话需要在server的上面添加:

upstream test {
         server 172.17.0.3:80;
         server 172.17.0.4:80;
     }
location / {
         proxy_pass http://test;
         index  index.php index.html index.htm;
     }

上面意思为172.17.0.3和172.17.0.4做负载均衡

以上内容纯属原创,翻版必究!不明白的可以加QQ群讨论:955496382

发表评论

登录... 后才能评论