当前大并发的网站基本都采用了Nginx来做代理服务器,并且做缓存,来扛住大并发。先前也用Nginx配置过简单的代理,今天有时间把整合过程拿出来和大家分享,不过其中大部分也是网上找来的资源。
Nginx完整的反向代理代码如下所示:
[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; #跟后端服务器连接超时时间,发起握手等候响应超时时间
proxy_read_timeout 600; #连接成功后,等候后端服务器响应时间,在后端排队中等候
proxy_send_timeout 600; #后端服务器数据回传时间,就是在规定时间内后端服务器必须传完所有数
proxy_buffer_size 16k; #代理请求缓存区,这个缓存区间会保存用户的信息以供nginx进行规则处理,一般只要能保存下头信息即可
proxy_buffers 4 32k; #同上,告诉nginx保存单个用的几个Buffer最大用多大空间
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;
}
#第一个虚拟主机,反向代理php_server_pool这组服务器
server {
listen 80;
server_name ;
access_log /var/log/nginx/www.linuxidc.net_access.log main;
location / {
#如果后端服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass ;
proxy_set_header Host ;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
#第二个虚拟主机
server {
listen 80;
server_name bbs.linuxidc.net;
access_log /var/log/nginx/www.linuxidc.net_access.log main;
#访问***地址,反向代理message_server_pool这组服务器
location /message/ {
proxy_pass ;
proxy_set_header Host $host;
}
#访问除了/message/之外的***地址,反向代理php_server_pool这组服务器
location /message/ {
proxy_pass ;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
#第三个虚拟主机
server {
listen 80;
server_name forum.linuxidc.net;
access_log /var/log/nginx/www.linuxidc.net_access.log main;
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass ;
proxy_set_header Host ;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}