Nginx反向代理以及配置优化
05-03
下面配置包含了,nginx配置的一个比较全面的反向代理的例子:
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