nginx配置备忘2
#nginx.conf
#启动 到目录下执行
#启动 到目录下执行
#start nginx 开启
#nginx.exe -s quit 关闭
#nginx.exe -s reload 重启
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
#window下使用nginx,如果要设置worker_connections大于1024 到这里下载win版本http://nginx-win.ecsds.eu/download/
#window下使用nginx,如果要设置worker_connections大于1024 到这里下载win版本http://nginx-win.ecsds.eu/download/
worker_connections 4096;
#如果multi_accept被禁止了,nginx一个工作进程只能同时接受一个新的连接。否则,一个工作进程可以同时接受所有的新连接。
multi_accept on;
}
#如果multi_accept被禁止了,nginx一个工作进程只能同时接受一个新的连接。否则,一个工作进程可以同时接受所有的新连接。
multi_accept on;
}
http {
#设置服务器组 后面用到
# ip_hash;(訪问ip) hash $remote_addr consistent; consistent使增加服务器时大部分seesion仍然一致
# 每一个请求按訪问ip的hash结果分配。这样每一个訪客固定訪问一个后端服务器,能够解决session的问题。如果被分配的服务器down了则转发到组内其他服务器
upstream go_server_group{
#这两个地址的8081端口下有相同的http服务
#这两个地址的8081端口下有相同的http服务
hash $remote_addr consistent;
server 127.0.0.1:8081;
server 192.200.0.244:8081;
server 192.200.0.244:8081;
}
#目前就一台 这里其实不用设置
upstream php_xxpt{
hash $remote_addr consistent;
server 127.0.0.1:18080 ;
}
charset utf-8;
tcp_nodelay on;
log_not_found off;
types_hash_max_size 2048;
types_hash_bucket_size 64;
client_max_body_size 16M;
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#==客户端链接超时时间
keepalive_timeout 65;
#keepalive_requests 10000;
#gzip on;
# 请求Limits
limit_req_log_level warn;
limit_req_zone $binary_remote_addr zone=ip_limit:10m rate=100r/s; #这里定义ip_limit,单个ip每秒100次请求,后面使用
#当配置多个server节点时,默认server names的缓存区大小就不够了,需要手动设置大一点
server_names_hash_bucket_size 512;
#server表示虚拟主机可以理解为一个站点,可以配置多个server节点搭建多个站点
#每一个请求进来确定使用哪个server由server_name确定
map $http_upgrade $connection_upgrade {
default upgrade;
"" close;
}
map $remote_addr $proxy_forwarded_elem {
# IPv4 addresses can be sent as-is
~^[0-9.]+$ "for=$remote_addr";
# IPv6 addresses need to be bracketed and quoted
~^[0-9A-Fa-f:.]+$ "for=\"[$remote_addr]\"";
# Unix domain socket names cannot be represented in RFC 7239 syntax
default "for=unknown";
}
map $http_forwarded $proxy_add_forwarded {
default "$proxy_forwarded_elem";
}
server {
listen 80;
server_name localhost;
#注意加斜杆
location /go/{
limit_req zone=ip_limit; #前面定义ip_limit,这里使用
proxy_pass http://go_server_group/;
#注意加斜杆 http://127.0.0.1/go/test 等于 http://127.0.0.1:8081/test ; 字符go没有传参
#注意加斜杆 http://127.0.0.1/go/test 等于 http://127.0.0.1:8081/test ; 字符go没有传参
include proxy.conf;
}
location /index.php{
proxy_pass http://php_xxpt/index.php;
include proxy.conf;
}
location /public{
proxy_pass http://php_xxpt/public;
include proxy.conf;
}
location /demo1 {
proxy_pass http://192.200.0.3/demo1;
#http://127.0.0.1/demo1/index.php ; 等于 http://192.200.0.3/demo1/index.php ; demo1有传参
#http://127.0.0.1/demo1/index.php ; 等于 http://192.200.0.3/demo1/index.php ; demo1有传参
include proxy.conf;
}
location =/ {
return 404;
}
location / {
return 503;
}
}
server_tokens off; #隐藏nginx版本号
server_tokens off; #隐藏nginx版本号
}
------------------------------------------------------------------------------------------------
#proxy.conf
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
# Proxy headers
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Forwarded $proxy_add_forwarded;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
# Proxy timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
#隐藏 X-Powered-By
proxy_hide_header X-Powered-By;