安全问题,建议用nobody,不要用root.
user root;
worker数和服务器的cpu数相等是最为适宜
worker_processes 1;
error_log path(存放路径) level(日志等级)path表示日志路径,level表示日志等级,
具体如下:[ debug | info | notice | warn | error | crit ]
从左至右,日志详细程度逐级递减,即debug最详细,crit最少,默认为crit。
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
#这个值是表示每个worker进程所能建立连接的最大值,所以,一个nginx能建立的最大连接数,
#应该是worker\_connections * worker\_processes。
#当然,这里说的是最大连接数,对于HTTP请求本地资源来说,能够支持的最大并发数量是worker\_connections * worker\_processes,
#如果是支持http1.1的浏览器每次访问要占两个连接,
#所以普通的静态访问最大并发数是: worker\_connections * worker\_processes /2,
#而如果是HTTP作为反向代理来说,最大并发数量应该是worker\_connections * worker\_processes/4。
#因为作为反向代理服务器,每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接。
worker_connections 1024;
#这个值是表示nginx要支持哪种多路io复用。
#一般的Linux选择epoll, 如果是(*BSD)系列的Linux使用kquene。
#windows版本的nginx不支持多路IO复用,这个值不用配。
use epoll;
# 当一个worker抢占到一个链接时,是否尽可能的让其获得更多的连接,默认是off 。
multi_accept on;
# 默认是on ,开启nginx的抢占锁机制。
accept_mutex on;
}
tcp/udp 代理
stream {
upstream backend {
# 1、轮询(默认)
# 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
# 2、指定权重
# 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
#3、IP绑定 ip_hash
# 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
#4、备机方式 backup
# 正常情况不访问设定为backup的备机,只有当所有非备机全都宕机的情况下,服务才会进备机。
#5、fair(第三方)
#按后端服务器的响应时间来分配请求,响应时间短的优先分配。
#6、url_hash(第三方)
#按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
#还可以设置请求失败的次数。max_fails
#hash $remote_addr consistent;
# 代理tcp,都代理到3306端口,但是weight(权重)不一样,代理到252服务的概率为50%,代理到251服务的概率为30%.
server 192.168.1.252:3306 weight=5 max\_fails=3 fail\_timeout=30s;
server 192.168.1.253:3306 weight=2 max\_fails=3 fail\_timeout=30s;
}
#配置tcp
server {
listen 3306;
proxy_connect_timeout 3s;
proxytimeout 10s; proxypass backend;
}
upstream dnsservers{ server 192.168.47.44:53; } #配置udp server { listen 53 udp; #允许访问的IP allow 192.168.21.216; #除216之外的IP都不允许访问 deny all; #UDP traffic will be proxied to the "dnsservers" upstream group
proxy_pass dns_servers;
}
}
http {
# lua 脚本的配置
lua\_package\_path "/usr/local/src/nginx/conf/waf/?.lua";
lua\_shared\_dict limit 10m;
init\_by\_lua_file /usr/local/src/nginx/conf/waf/init.lua;
access\_by\_lua_file /usr/local/src/nginx/conf/waf/waf.lua;
#当web服务器收到静态的资源文件请求时,依据请求文件的后缀名在服务器的MIME配置文件中找到对应的MIME Type,再根据MIME Type设置HTTP Response的Content-Type,然后浏览器根据Content-Type的值处理文件。
include mime.types;
#如果 不能从mime.types找到映射的话,用以下作为默认值
default_type application/octet-stream;
#关闭反向代理缓存
proxy_buffering off;
#log\_format main '$remote\_addr - $remote\_user \[$time\_local\] "$request" '
# '$status $body\_bytes\_sent "$http_referer" '
# '"$http\_user\_agent" "$http\_x\_forwarded_for"';
#access_log logs/access.log main;
#开启从磁盘直接到网络的文件传输,适用于有大文件上传下载的情况,提高IO效率。
sendfile on;
#tcp_nopush on;
#一个请求完成之后还要保持连接多久, 默认为0,表示完成请求后直接关闭连接。
#keepalive_timeout 0;
keepalive_timeout 65;
#开启或者关闭gzip模块
#gzip on;
#设置允许压缩的页面最小字节数,页面字节数从header头中的Content-Length中进行获取。
#gzip_min_lenth 1k;
# gzip压缩比,1 压缩比最小处理速度最快,9 压缩比最大但处理最慢(传输快但比较消耗cpu)
#gzip\_comp\_level 4;
#匹配MIME类型进行压缩,(无论是否指定)"text/html"类型总是会被压缩的。
#gzip_types types text/plain text/css application/json application/x-javascript text/xml
#动静分离
#服务器端静态资源缓存,最大缓存到内存中的文件,不活跃期限
open\_file\_cache max=655350 inactive=20s;
#活跃期限内最少使用的次数,否则视为不活跃。
open\_file\_cache\_min\_uses 2;
#验证缓存是否活跃的时间间隔
open\_file\_cache_valid 30s;
server {
listen 81;
server_name nginx.test1.com;
#charset koi8-r;
#access_log logs/host.access.log main;
#location \[=|~|~*|^~\] /uri/ { … }
# = 精确匹配
# ~ 正则匹配,区分大小写
# ~\* 正则匹配,不区分大小写
# ^~ 关闭正则匹配
#匹配原则:
# 1、所有匹配分两个阶段,第一个叫普通匹配,第二个叫正则匹配。
# 2、普通匹配,首先通过“=”来匹配完全精确的location
# 2.1、 如果没有精确匹配到, 那么按照最大前缀匹配的原则,来匹配location
# 2.2、 如果匹配到的location有^~,则以此location为匹配最终结果,如果没有那么会把匹配的结果暂存,继续进行正则匹配。
# 3、正则匹配,依次从上到下匹配前缀是~或~*的location, 一旦匹配成功一次,则立刻以此location为准,不再向下继续进行正则匹配。
# 4、如果正则匹配都不成功,则继续使用之前暂存的普通匹配成功的location.
# 匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配。
location / {
#定义服务器的默认网站根目录位置
root html;
#默认访问首页索引文件的名称
index index.html index.htm;
#反向代理路径
proxy_pass http://192.168.2.3/test;
#反向代理的超时时间
proxy\_connect\_timeout 10;
proxy_redirect default;
}
# auth\_request表示需要调用第三方接口进行授权,后台response.setStatus(200)返回时会继续走proxy\_pass 代理。
location /test {
auth_request /auth;
proxy_pass http://192.168.40.26:8080/cloud/wapi/v1.0/hello;
}
location /auth {
proxy_pass http://192.168.40.26:8080/cloud/wapi/v1.0/auth/;
#proxy\_pass\_request_body off;
#proxy\_set\_header Content-Length "";
#proxy\_set\_header X-Original-URI $request_uri;
}
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
proxy\_set\_header Host $host;
proxy\_set\_header Referer $http_referer;
proxy\_set\_header Cookie $http_cookie;
proxy\_set\_header X-Real-IP $remote_addr;
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;
# 访问test2时,会通过lua脚本的access\_by\_lua调用后台方法v1.0/auth方法。
location /test2 {
access\_by\_lua 'local res = ngx.location.capture("/v1.0/auth")
if res.status == ngx.HTTP_OK then
return
end
if res.status == ngx.HTTP_FORBIDDEN then
ngx.exit(res.status)
end
ngx.exit(ngx.HTTP\_INTERNAL\_SERVER_ERROR)
';
proxy_pass http://192.168.40.119:8888/ipam-server;
}
#调用后台方法v1.0/auth方法 时会被代理到192.168.40.26:8080/v1.0/auth上去。
location ~ /v1.0/(.*) {
internal;
proxy_pass http://192.168.40.26:8080;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \\.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \\.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi\_param SCRIPT\_FILENAME /scripts$fastcgi\_script\_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
server {
listen 443 ssl;
server_name nginx.test2.com;
# 通过 openssl 生成对应的证书
ssl_certificate /usr/local/src/nginx/ssl/nginx.crt;
ssl\_certificate\_key /usr/local/src/nginx/ssl/nginx.key;
ssl\_session\_cache shared:SSL:1m;
ssl\_session\_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl\_prefer\_server_ciphers on;
location = /favicon.ico {
log\_not\_found off;
access_log off;
}
# https的代理单点登录
location /cas/login {
proxy\_set\_header Host $host:$server_port;
proxy\_set\_header Referer $http_referer;
proxy\_set\_header Cookie $http_cookie;
proxy\_set\_header X-Real-IP $remote_addr;
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;
proxy\_set\_header X-FORWARDED-HOST $server_addr;
proxy\_set\_header X-FORWARDED-PORT $server_port;
# 单点登录的服务器的IP
proxy_pass https://192.168.40.119:8090/cas/login;
}
# 登录单点登录时所需的css、js、jpg等文件
location ~*\\.(gif|jpg|jpeg|png|css|js|jsp|htm)$ {
# 此处不能配置 https://192.168.40.119:8090/ 否则会报错
proxy_pass https://192.168.40.119:8090;
}
}
}