Detailed nginx HTTP load balancing and reverse proxy configuration _nginx

Source: Internet
Author: User
Tags php web server port number nginx reverse proxy

The current large concurrent website basically uses Nginx to do proxy server, and do cache, to carry the big concurrency. Previously also used Nginx configured a simple agent, today have time to take out the integration process and share, but most of them are also online to find resources.

Nginx The complete reverse proxy code looks like this:

[Root@data conf]# vim nginx.conf user www www. 
  
Worker_processes 10; 
  
Error_log/var/log/nginx/nginx_error.log; 
  
PID Logs/nginx.pid; 
  Worker_rlimit_nofile 65535 Events {use Epoll; 
Worker_connections 65535; 
  } http {include mime.types; 
   
  Default_type Application/octet-stream; 
  Server_names_hash_bucket_size 128; 
  Client_header_buffer_size 32k; 
  Large_client_header_buffers 4 32k; 
  Sendfile on; 
  
  Tcp_nopush on; 
  Keepalive_timeout 65; 
  Fastcgi_connect_timeout 300; 
  Fastcgi_send_timeout 300; 
  Fastcgi_read_timeout 300; 
  Fastcgi_buffer_size 64k; 
  Fastcgi_buffers 4 64k; 
  Fastcgi_busy_buffers_size 128k; 
  
  Fastcgi_temp_file_write_size 128k; 
  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/css application/xml; 
  
  Gzip_vary on;  Client_max_body_size 300m; #允许客户端请求的最大单个文件字节数 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数 Proxy_connect_timeout 600;  #跟后端服务器连接超时时间, initiate handshake wait for response timeout Proxy_read_timeout 600; #连接成功后, wait for back-end server response time, wait for Proxy_send_timeout 600 in the back-end queue; #后端服务器数据回传时间, that is, in the specified time the backend server must pass through all the number proxy_buffer_size 16k; #代理请求缓存区, this cache interval will save the user's information for Nginx rules processing, generally as long as can save the head information can proxy_buffers 4 32k; #同上, tell Nginx to save a single use of several buffer maximum space proxy_busy_buffers_size 54k; #如果系统很忙可以申请用的几个更大的proxy_buffer proxy_temp_file_write_size 64k; 
  #缓存临时文件大小 upstream Php_server_pool {server 192.168.1.100:80 weight=4 max_fails=2 fail_timeout=30s; 
  Server 192.168.1.101:80 weight=4 max_fails=2 fail_timeout=30s; 
Server 192.168.1.102:80 weight=4 max_fails=2 fail_timeout=30s; 
  } upstream Message_server_pool {server 192.168.1.103:3245; 
Server 192.168.1.104:3245 down; 
  } upstream Bbs_server_pool {server 192.168.1.105:80 weight=4 max_fails=2 fail_timeout=30s; 
  Server 192.168.1.106:80 weight=4 max_fails=2 fail_timeout=30s; Server 192.168.1.107:80 weight=4 max_fails=2 fail_timeout=30S 
Server 192.168.1.108:80 weight=4 max_fails=2 fail_timeout=30s; 
    #第一个虚拟主机, the reverse proxy php_server_pool This set of server {Listen 80; 
    server_name www.chlinux.net; 
    Access_log/var/log/nginx/www.chlinux.net_access.log main; 
   Location/{#如果后端服务器返回502, 504, execution timeout error, automatically forward the request to another server in the upstream load balancing pool for failover. 
   Proxy_next_upstream http_502 http_504 error timeout invalid_header; 
   Proxy_pass Http://php_server_pool; 
   Proxy_set_header Host www.chlinux.net; 
    Proxy_set_header x-forwarded-for $remote _addr; 
    }} #第二个虚拟主机 server {listen 80; 
    server_name bbs.chlinux.net; 
Access_log/var/log/nginx/www.chlinux.net_access.log main; #访问http://bbs.chlinux.net/message/*** address, reverse proxy Message_server_pool This set of servers location/message/{Proxy_pass http://mes 
     Sage_server_pool; 
    Proxy_set_header Host $host; #访问除了 http://bbs.chlinux.net/*** address outside/message/, reverse proxy Php_server_pool This set of servers location/message/{Proxy_pass http: 
  Bbs_server_pool;   Proxy_set_header Host $host; 
    Proxy_set_header x-forwarded-for $remote _addr; 
    }} #第三个虚拟主机 server {listen 80; 
    server_name forum.chlinux.net; 
    Access_log/var/log/nginx/www.chlinux.net_access.log main; 
     Location/{proxy_next_upstream http_502 http_504 error timeout invalid_header; 
     Proxy_pass Http://php_server_pool; 
     Proxy_set_header Host www.chlinux.net; 
    Proxy_set_header x-forwarded-for $remote _addr;
 } 
  } 
}

As shown above, you have seen how the Nginx is configured for load balancing for multiple domain names. The pustream directive is used to set up a set of proxy servers that can be used in proxy_pass and fastcgi_pass directives, and the server directive in the upstream module is used to specify the name and parameters of the back-end server, which can be a domain name, an IP address , port number, or Unix Socket

Nginx Reverse Proxy can configure dynamic, static Web page separation, is to let dynamic PHP and other programs Web page access to the PHP Web server, so that caching pages, pictures, JavaScript, CSS, flash access squid, such as caching servers.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.