#user nobody;
#nginx线程数量
worker_processes 8;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
#nginx最大连接数
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
#nginx日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$resp_body"';
#关闭access_log
#access_log off;
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#保持连接时间;
keepalive_timeout 65;
#php服务
server {
listen 80;
server_name 10.130.161.19;
index index.html index.htm index.php;
root /opt/local/phpServer;
access_log off;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
}
#配置upstream
upstream httptest {
server 127.0.0.1:9011 weight=1;
server 127.0.0.1:9012 weight=1;
}
upstream websocketTest {
server 127.0.0.1:9021 weight=1;
}
server {
listen 80;
#配置域名
server_name test.com;
#此为配置websocket即ws://的方式
location ^~ /websocket-test/ {
proxy_pass http://websocketTest;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
#此为配置普通http的方式
location ^~ /http-test/ {
#以下三行为跨域配置项,当前端使用nodejs时会用到,也可直接配在server下面
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'X-Content-Sender,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
proxy_pass http://httptest;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#https配置方式
server {
listen 443 ssl;
server_name 10.130.161.19;
ssl on;
ssl_certificate /opt/local/nginx/ssl/nginx42.crt;
ssl_certificate_key /opt/local/nginx/ssl/nginx42.key;
ssl_session_cache shared:SSL:10m;#此处为缓存ssl证书容量,据官方文档所述,cache中的1m可以存放4000个session
ssl_session_timeout 10m;
set $resp_body "";
location ^~ /https-test/ {
proxy_pass http://httptest;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#此处为打印返回值,跟上面的 set $resp_body一起配置
#lua_need_request_body on;
# body_filter_by_lua '
# local resp_body = string.sub(ngx.arg[1], 1, 1000)
# ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
# if ngx.arg[2] then
# ngx.var.resp_body = ngx.ctx.buffered
# end
# ';
}
}
}