Nginx HTTP load balancing and reverse proxy configuration

Source: Internet
Author: User
Tags php web server nginx reverse proxy nginx load balancing

The current large concurrent sites are basically using Nginx to do proxy server, and do cache, to carry large concurrency. Previously also used Nginx configured a simple agent, today there is time to take the integration process to share with you, but most of them are online resources to find.

The full reverse proxy code for Nginx is as follows:

[email protected] conf]# vim nginx.conf user www www; Worker_processesTen; Error_log/var/log/nginx/Nginx_error.log; PID Logs/Nginx.pid; Worker_rlimit_nofile65535Events {use Epoll; Worker_connections65535;      } http {include mime.types; Default_type Application/octet-stream; Server_names_hash_bucket_size -;      Client_header_buffer_size 32k; Large_client_header_buffers432k;      Sendfile on;         Tcp_nopush on; Keepalive_timeout $; Fastcgi_connect_timeout -; Fastcgi_send_timeout -; Fastcgi_read_timeout -;      Fastcgi_buffer_size 64k; Fastcgi_buffers464k;      Fastcgi_busy_buffers_size 128k;         Fastcgi_temp_file_write_size 128k;      gzip on;      Gzip_min_length 1k; Gzip_buffers416k; Gzip_http_version1.0; Gzip_comp_level2; 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 -; #跟后端服务器连接超时时间, initiates a handshake waiting for a response timeout proxy_read_timeout -; #连接成功后, waiting for back-end server response time, waiting for proxy_send_timeout in the backend queue -, #后端服务器数据回传时间, that is, in the specified time, the backend server must pass all the number of proxy_buffer_size 16k; #代理请求缓存区, this cache interval will save the user's information for Nginx rules processing, generally as long as the head information can be saved Proxy_buffers432k, #同上, tell Nginx to save a single use of a few buffer max with how much space proxy_busy_buffers_size 54k; #如果系统很忙可以申请用的几个更大的proxy_buffer Proxy_tem P_file_write_size 64k; #缓存临时文件大小 upstream Php_server_pool {server192.168.1.100: theweight=4max_fails=2fail_timeout=30s; Server192.168.1.101: theweight=4max_fails=2fail_timeout=30s; Server192.168.1.102: theweight=4max_fails=2fail_timeout=30s; } upstream Message_server_pool {server192.168.1.103:3245; Server192.168.1.104:3245Down ; } upstream Bbs_server_pool {server192.168.1.105: theweight=4max_fails=2fail_timeout=30s; Server192.168.1.106: theweight=4max_fails=2fail_timeout=30s; Server192.168.1.107: theweight=4max_fails=2fail_timeout=30s; Server192.168.1.108: theweight=4max_fails=2fail_timeout=30s; #第一个虚拟主机, the reverse proxy php_server_pool This set of server {Listen the;          server_name www.chlinux.net; Access_log/var/log/nginx/Www.chlinux.net_access.log Main; Location/{#如果后端服务器返回502,504, perform timeouts and other errors, and automatically forward requests to another server in the upstream load balancer 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 the;          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 serverslocation/message/{proxy_pass http://Message_server_pool; Proxy_set_header Host $host; } #访问除了HTTP outside of/message/://bbs.chlinux.net/*** Address, reverse proxy Php_server_pool This set of serverslocation/message/{proxy_pass http://Bbs_server_pool; Proxy_set_header Host $host; Proxy_set_header X-forwarded-For $remote _addr; }} #第三个虚拟主机 Server {Listen the;          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, we have seen how the nginx load balancing for multiple domain names is configured. The pustream directive is used to set up a set of proxy servers that can be used in the Proxy_pass and Fastcgi_pass directives, and the server directives in the upstream module are used to specify the name and parameters of the backend server, which can be a domain name, an IP address , port number, or Unix Socket

Nginx reverse proxy can be configured dynamic, static Web page separation, is to let dynamic PHP and other programs Web page access to PHP Web server, let cache pages, images, JavaScript, CSS, flash access to squid and other cache server.

Nginx HTTP load balancing and reverse proxy configuration

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.