如果你下载好啦,你的安装文件,不妨打开conf文件夹的nginx.conf文件,Nginx服务器的基础配置,默认的配置也存放在此。
在nginx.conf的注释符号位#
nginx文件的结构,这个对刚入门的同学,可以多看两眼。
默认的config
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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 localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
nginx文件结构
... #全局块
events { #events块
...
}
http #http块
{
... #http全局块
server #server块
{
... #server全局块
location [PATTERN] #location块
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局块
}
1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。
5、location块:配置请求的路由,以及各种页面的处理情况。
下面给大家上一个配置文件,作为理解,同时也配入我搭建的一台测试机中,给大家示例。
########### 每个指令必须有分号结束。#################
#配置用户或者组,默认为nobody nobody。
#user administrator administrators;
#允许生成的进程数,默认为1
#worker_processes 2;
#指定nginx进程运行文件存放地址
#pid /nginx/pid/nginx.pid;
#制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:#debug|info|notice|warn|error|crit|alert|emerg
error_log logs/error.log debug;
#最大文件打开数(连接),可设置为系统优化后的ulimit -HSn的结果
worker_rlimit_nofile 51200;
events {
#设置网路连接序列化,防止惊群现象发生,默认为on
accept_mutex on;
#设置一个进程是否同时接受多个网络连接,默认为off
multi_accept on;
#事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
#use epoll;
#最大连接数,默认为512
worker_connections 1024;
}
http {
#文件扩展名与文件类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream;
#limit模块,可防范一定量的DDOS攻击
#用来存储session会话的状态,如下是为session分配一个名为one的10M的内存存储区,限制了每秒只接 受一个ip的一次请求 1r/s
#limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
#limit_conn_zone $binary_remote_addr zone=addr:10m;
#第三方模块lua防火墙
#lua_need_request_body on;
#lua_shared_dict limit 50m;
#lua_package_path "/application/nginx/conf/waf/?.lua";
#init_by_lua_file "/application/nginx/conf/waf/init.lua";
#access_by_lua_file "/application/nginx/conf/waf/access.lua";
#设定请求缓存
server_names_hash_bucket_size 128;
client_header_buffer_size 512k;
large_client_header_buffers 4 512k;
client_max_body_size 100m;
#隐藏响应header和错误通知中的版本号
server_tokens off;
#取消服务日志
#access_log off;
#自定义格式
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for';
#combined为日志格式的默认值
access_log logs/access.log myFormat;
#开启高效传输模式,允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile on;
#每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
sendfile_max_chunk 100k;
#连接超时时间,默认为75s,可以在http,server,location块。
keepalive_timeout 65;
#激活tcp_nopush参数可以允许把httpresponse header和文件的开始放在一个文件里发布,积极的作用是减少网络报文段的数量
#tcp_nopush on;
#激活tcp_nodelay,内核会等待将更多的字节组成一个数据包,从而提高I/O性能
#tcp_nodelay on;
#FastCGI相关参数:为了改善网站性能:减少资源占用,提高访问速度
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压缩功能
gzip on;
#设置允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取。默认值是0,表示不管页面多大都进行压缩。建议设置成大于1K。如果小于1K可能会越压越大。
gzip_min_length 1k;
#压缩缓冲区大小。表示申请4个单位为16K的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果。
gzip_buffers 4 16k;
#压缩版本(默认1.1,前端为squid2.5时使用1.0)用于设置识别HTTP协议版本,默认是1.1,目前大部分浏览器已经支持GZIP解压,使用默认即可。
gzip_http_version 1.0;
#压缩比率。用来指定GZIP压缩比,1压缩比最小,处理速度最快;9压缩比最大,传输速度快,但处理最慢,也比较消耗cpu资源。
gzip_comp_level 9;
#用来指定压缩的类型,“text/html”类型总是会被压缩
gzip_types text/plain application/x-javascript text/css application/xml;
#vary header支持。该选项可以让前端的缓存服务器缓存经过GZIP压缩的页面,例如用Squid缓存经过Nginx压缩的数据。
gzip_vary off;
#开启ssi支持,默认是off
ssi on;
ssi_silent_errors on;
#反向代理负载均衡设定部分
#upstream表示负载服务器池,定义名字为backend_server的服务器池
upstream backend_server {
server 10.254.244.20:81 weight=1 max_fails=2 fail_timeout=30s;
server 10.254.242.40:81 weight=1 max_fails=2 fail_timeout=30s;
server 10.254.245.19:81 weight=1 max_fails=2 fail_timeout=30s;
server 10.254.243.39:81 weight=1 max_fails=2 fail_timeout=30s;
#设置由 fail_timeout 定义的时间段内连接该主机的失败次数,以此来断定 fail_timeout 定义的时间段内该主机是否可用。默认情况下这个数值设置为 1。零值的话禁用这个数量的尝试。
#设置在指定时间内连接到主机的失败次数,超过该次数该主机被认为不可用。
#这里是在30s内尝试2次失败即认为主机不可用!
}
#定义的服务器列表
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #热备
}
error_page 404 https://www.baidu.com; #错误页
server {
#单连接请求上限次数。
keepalive_requests 120;
#监听端口
listen 4545;
#监听地址
server_name 127.0.0.1;
#server_name www.abc.com abc.com;
#error_page 500 502 404 /templates/kumi/phpcms/404.html; #错误页面
# 或者重定向405错误
error_page 405 =200 @405;
location @405 {
root $rootpath;
proxy_method GET;
proxy_pass http://static_backend;
allow all;
}
#伪静态 将www.abc.com/list....html的文件转发到index.php。。。
#rewrite ^/list-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /index.php?m=content&c=index&a=lists&catid=$1&types=$2&country=$3&language=$4&age=$5&startDate=$6&typeLetter=$7&type=$8&page=$9 last;
#请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
location ~*^.+$ { '
#根目录
#root path;
#设置默认页
#index vv.txt;
#请求转向mysvr 定义的服务器列表
proxy_pass http://mysvr;
#拒绝的ip
deny 127.0.0.1;
#禁止其他ip访问
#deny all;
#允许的ip
allow 172.18.5.54;
}
#将符合js,css文件的等设定expries缓存参数,要求浏览器缓存。
location~ .*\.(js|css)?$ {
#客户端缓存上述js,css数据30天
expires 30d;
}
# 故障转移
location ~ ^/list {
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 200 301 302 304 1d;
#proxy_cache_valid any 1d;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";
#proxy_ignore_headers Set-Cookie;
#proxy_hide_header Set-Cookie;
proxy_pass http://backend_server;
add_header Nginx-Cache "$upstream_cache_status from km";
expires 1d;
}
##add by 20140321#######nginx防sql注入##########
###start####
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 444;
}
if ($query_string ~* ".* (insert|select|delete|update|count|\*|%|master|truncate|declare|\'|\;|and|or|\(|\)|exec).* ")
{
return 444;
}
if ($request_uri ~* "(cost\()|(concat\()") {
return 444;
}
if ($request_uri ~* "[+|(%20)]union[+|(%20)]") {
return 444;
}
if ($request_uri ~* "[+|(%20)]and[+|(%20)]") {
return 444;
}
if ($request_uri ~* "[+|(%20)]select[+|(%20)]") {
return 444;
}
set $block_file_injections 0;
if ($query_string ~ "[a-zA-Z0-9_]=(\.\.//?)+") {
set $block_file_injections 1;
}
if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") {
set $block_file_injections 1;
}
if ($block_file_injections = 1) {
return 448;
}
set $block_common_exploits 0;
if ($query_string ~ "(<|%3C).*script.*(>|%3E)") {
set $block_common_exploits 1;
}
if ($query_string ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})") {
set $block_common_exploits 1;
}
if ($query_string ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") {
set $block_common_exploits 1;
}
if ($query_string ~ "proc/self/environ") {
set $block_common_exploits 1;
}
if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3D)") {
set $block_common_exploits 1;
}
if ($query_string ~ "base64_(en|de)code\(.*\)") {
set $block_common_exploits 1;
}
if ($block_common_exploits = 1) {
return 444;
}
set $block_spam 0;
if ($query_string ~ "\b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)\b") {
set $block_spam 1;
}
if ($query_string ~ "\b(erections|hoodia|huronriveracres|impotence|levitra|libido)\b") {
set $block_spam 1;
}
if ($query_string ~ "\b(ambien|blue\spill|cialis|cocaine|ejaculation|erectile)\b") {
set $block_spam 1;
}
if ($query_string ~ "\b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)\b") {
set $block_spam 1;
}
if ($block_spam = 1) {
return 444;
}
set $block_user_agents 0;
if ($http_user_agent ~ "Wget") {
set $block_user_agents 1;
}
# Disable Akeeba Remote Control 2.5 and earlier
if ($http_user_agent ~ "Indy Library") {
set $block_user_agents 1;
}
# Common bandwidth hoggers and hacking tools.
if ($http_user_agent ~ "libwww-perl") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "GetRight") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "GetWeb!") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "Go!Zilla") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "Download Demon") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "Go-Ahead-Got-It") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "TurnitinBot") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "GrabNet") {
set $block_user_agents 1;
}
if ($block_user_agents = 1) {
return 444;
}
###end########
}
-----------------------ssl(https)相关------------------------------------
server {
listen 13820; #监听端口
server_name localhost;
charset utf-8; #gbk,utf-8,gb2312,gb18030 可以实现多种编码识别
ssl on; #开启ssl
ssl_certificate /ls/app/nginx/conf/mgmtxiangqiankeys/server.crt; #服务的证书
ssl_certificate_key /ls/app/nginx/conf/mgmtxiangqiankeys/server.key; #服务端key
ssl_client_certificate /ls/app/nginx/conf/mgmtxiangqiankeys/ca.crt; #客户端证书
ssl_session_timeout 5m; #session超时时间
ssl_verify_client on; # 开户客户端证书验证
ssl_protocols SSLv2 SSLv3 TLSv1; #允许SSL协议
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; #加密算法
ssl_prefer_server_ciphers on; #启动加密算法
access_log /lw/logs/nginx/dataadmin.test.com.ssl.access.log access ; #日志格式及日志存放路径
error_log /lw/logs/nginx/dataadmin.test.com.ssl.error.log; #错误日志存放路径
}
-------------------------------------------------------------------------
}
上面是nginx的基本配置,需要注意的有以下几点:
1、1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址; 2.$remote_user :用来记录客户端用户名称; 3.$time_local : 用来记录访问时间与时区;4.$request : 用来记录请求的url与http协议;
5.$status : 用来记录请求状态;成功是200, 6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;7.$http_referer :用来记录从那个页面链接访问过来的; 8.$http_user_agent :记录客户端浏览器的相关信息;
2、惊群现象:一个网路连接到来,多个睡眠的进程被同事叫醒,但只有一个进程能获得链接,这样会影响系统性能。
3、每个指令必须有分号结束。
其他参考配置如下:
#监听的端口
listen 8088;
#监听的地址
server_name 192.168.22.143;
#access_log logs/host.access.log;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 重定向405错误
error_page 405 =200 @405;
location @405 {
root $rootpath;
proxy_method GET;
proxy_pass http://static_backend;
allow all;
}
add_header Cache-Control no-cache;
add_header Cache-Control private;
######################### 设置全局变量 ##########################
# bundle的扩展目录名
set $extend "extend";
# bundle的当前目录名(开发环境为src, 生产环境为dist)
set $switch "src";
# bundles的根目录,本地资源根目录设置
set $rootpath "E:\work\F1V3.0\f1-platform-modules";
# zuul的地址
set $zuulPath "http://192.168.1.20:8081";
# uaa授权服务器的地址
set $authorizationServicePath "http://192.168.1.20:8080";
# index的配置
set $indexPath "permission/views/team1/enter.html";
################# index配置 #######################################
# 定位到首页
location = / {
rewrite ^/(.*)$ /$indexPath permanent;
}
################# 组件包的引入(包括组件包和三方包)###############
# 平台前端组件包
location ^~ /public/ {
alias $rootpath/F1UI_public_libraries/dist/;
}
## 平台三方包
location ^~ /jquery/ {
alias $rootpath/F1UI_widget_libraries/dist/;
}
####################### 微服务静态资源路径配置 #####################
## new 微服务
location ^~ /new/ {
set $temple $extend;
alias $rootpath/F1UI_new_bundle/$temple/;
if (!-e $request_filename) {
set $temple $switch;
}
}
############################ 三方包配置 ##########################
location ^~ /plugins/ {
alias $rootpath/F1UI_plugins_libraries/;
}
###################### 微服务服务地址路径配置 ####################
#授权服务器
location ^~ /uaa {
proxy_pass $authorizationServicePath$request_uri;
}
#配置zuul
location ^~ /zuul/ {
proxy_pass $zuulPath$request_uri;
}
user www www; #运行用户和组
worker_processes number | auto; #worker进程数,cpu总核同数量,减少cpu调度成本。
worker_cpu_affinity 01 10; #多核绑定,位图表示法,ps -F查看,PSR列对应CPU号
pid /usr/local/webserver/nginx/nginx.pid; #指定pid存放的路径
worker_rlimit_nofile; #文件描述符数量
master_process on | off; #启动进程池机制,默认on,如果设置为off,将不会建立master进程,用一个worker来处理。worker_processes也会失效。
daemon on | off; #是否守护进程,默认on。
#任意域可配
error_log file | level #debug | info | notice | warn | error | crit | alert | emerg 默认error,过滤前面包含后面,debug需要编译--with-debug,debug按模块控制等级,例如debug_http。
events{
use poll; #开启多路复用IO(linux2.6内核以上)
worker_connections 65535; #单个worker进程最大连接并发数
debug_connection 192.168.1.0/24; #单个ip段进行debug
}
http{
include mime.types; #包含mime.types文件
include vhost/*.conf; #包含vhost下的所有虚拟主机配置文件
default_type application/octet-stream; #指定默认的MIME类型
client_max_body_size 8m; #客户端上传的文件大小
#设置日志格式
log_format name '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for '
'"$upstream_addr" "$upstream_status" "$upstream_response_time" "$request_time"';
#日志存放路径、格式和缓存大小,格式设定为上面定义的name
access_log /var/log/nginx/access.log name;
sendfile on; #sendfile是个比 read 和 write 更高性能的系统接口, 反向代理时候无效,因为文件句柄是 socket。
tcp_nopush on; #调用tcp_cork方法,这个也是默认的,结果就是数据包不会马上传送出去,等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞。
tcp_nodelay on; #合并小的TCP 包为一个,避免了过多的小报文的 TCP 头所浪费的带宽。如果开启了这个算法 (默认),则协议栈会累积数据直到以下两个条件之一满足的时候才真正发送出去,积累的数据量到达最大的 TCP Segment Size或收到了一个 Ack。
server_tokens off; #关闭nginx 版本号,修改版本号 vi src/core/nginx.h,NGINX_VER
gzip on; #启用压缩
gzip_static on; #启用HTTPGzipStatic模块(不在core和standard模块组中,但rpm安装带了此模块),该模块可以读取预先压缩的gz文件,这样可以减少每次请求进行gzip压缩的CPU资源消耗。
gzip_comp_level 5; #压缩级别,1最小最快,9最大最慢,一般设为3
gzip_min_length 1024; #压缩的最小长度,小于此长度的不压缩(此长度即header中的Content-Length)
gzip_types text/plain text/css application/x-javascript application/javascript application/xml; (什么类型的页面或文档启用压缩)
limit_zone myzone $binary_remote_addr 10m; #myzone 名字,$binary_remote_addr = $remore_addr,,10m的会话空间池
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s; #rate=1r/s 的意思是每个地址每秒只能请求一次,也就是说根据漏桶(leaky bucket)算法 burst=120 一共有120块令牌,并且每秒钟只新增1块令牌, 120块令牌发完后 多出来的那些请求就会返回503
server{
limit_conn one 1; #限制客户端并发连接数量为1
limit_req zone=req_one burst=120; #加上 nodelay之后超过 burst大小的请求就会直接 返回503
}
#反向代理时nginx需要访问的上游服务器和负载均衡策略
#加权轮询,基础策略,计算各个后端服务器的当前权值,选择得分最高的服务器处理当前请求。
#ip哈希,哈希选择失败20以上或一台后端服务器,采用加权。
#加权适用性强,不依赖客户端任何信息,能把客户端请求合理均匀的分配,劣势是同一个客户端多次请求会被分配到不同的后端服务器处理,无法满足会话保持。
#ip哈希较好地把同一客户端的多次请求分配到同一台后端服务器,劣势是某个ip地址请求特别多(大量用户通过一个nat代理请求),会导致某台后端服务器压力非常大,其他服务器很空闲的不均衡情况。
upstream back_end{
ip_hash; #负载均衡策略,默认使用加权轮询(round robin),官方内置,一致哈希、fair第三方模块。
server 127.0.0.1:80; #一台普通上游服务器
server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; #默认值1 和 30s,30s内连接3次失败,休息一会,30s后再来。
server UNIX:/tmp/backend3 backup; #备机,不可用时,不能使用ip_hash,扰乱哈希结果违背ip_hash策略初衷。
server 192.168.0.1:9000 down; #主动宕机,不参与被选。
server backend1.example.com weight=3; #权重3,默认1,与加权策略配合使用。
}
#取$is_args的值,定义为$my_flag,默认值为0,如果是“?”,值为1
map $is_args $my_flag{
default 0;
"?" 1;
}
#禁止用户通过ip地址访问服务器,nginx未配置该域名或通过服务器ip来访问采用。
server{
listen 80 default; #表示这个server是80端口默认的server
return 500; #表示任何使用这个server配置的请求将会返回500
}
#虚拟主机配置块
server{
listen 80; #监听80端口
server_name localhost | *.ibeiliao.com | *.* ; #主机名称,~开头的正则表达式
keepalive_timeout 75 | 0;#超时时间,默认75s,设为0自动断连, http、location、server域中。
# nginx 常用变量(按阶段执行,先统一set变量)
# curl -v -o /dev/null 'http://localhost/index.html?a=1&b=2' -H 'hello:world'
# $uri : 当前请求的uri,不包含?后的参数 = /index.html
# $is_args : 当前请求是否带参数,如果有参数值为?,否则是空字符串 =?
# $args : 当前请求的完整参数,即?后面的字符串 =a=1&b=2
# $request_uri : 当前请求的完整URI,包含参数 =/index.html?a=1&b=2
# $arg_xxx : 当前请求的某个参数 arg_a=1
# $http_xxx : 当前请求的xxx头部对应的值 http_hello= world
# $sent_http_xxx : 返回给客户端的响应头部对应的值 =nginx/1.8.0
#
# nginx 自定义变量
# set $max_size 10000;
# set $new_uri /v2$request_uri
# set $log_tag "extra action"
#
# location [ = | ~ | ~* | ^~ | @ ] uri { ... }
# 例如 location /image/ {...} 匹配 /image/001.jpg
# = : URI必须完全匹配
# ~ : 大小写敏感匹配 location ~ \.(php)$ {...} 大小写敏感处理php请求
# ~* : 大小写不敏感匹配 location ~* \.(png)$ {...} 忽略大小写,匹配所有的png文件
# ^~ : 匹配前半部分即可 location ^~ /image/ {...} 匹配/image/*.* 优先级比上面低。
# @ : 用于内部子请求,外部无法访问
#一个转发的location
location /passto {
proxy_set_header Host $host; #转发原始请求的host头部
proxy_buffering off; #禁用反向代理缓存,保证每次请求真实转发
proxy_pass http://back_end; #转发到upstream块定义的服务器集群
#能用try_files代替则用它,否则不要用if,官方声明if用不好的话会有bug。
#例子:优先使用person下的图片,目录不存在则使用公共目录的图片
if(-e "${document_root}/person"){
rewrite ^/(.*)$ /person/$1 break;
}
#try_files写法
try_files /person$uri /$uri =400;
}
location / { #匹配任意URI
root html; #http请求根目录,静态web服务器
alias /var/data/; #请求目录,与root不同的是,会把location当为别名。location /image/ {...},返回/var/data/001.jpg
index index.php; #默认index文件
auth_basic Auth; #弹出信息提示字符为Auth
auth_basic_user_file /etc/ngx_passwd; #账号密码
autoindex on; #自动列出目录,禁用index
autoindex_exact_size on; #设置索引文件大小的单位
autoindex_localtime on; #开启本地时间显示文件时间
}
#增加websocket的支持
location /wsapp/ {
proxy_pass http://wsbackend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location = /50x.html {
root html;
}
location ~ ^(.*)\/\.svn\/{ #禁止访问SVN配置文件
deny all; #禁止该匹配下的所有访问
deny 192.168.10.1; #禁止该ip访问,403 forbidden错误。
}
#下列文件缓存在本地浏览器30天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${
expires 30d;
}
#下列文件缓存在本地浏览器1小时
location ~ .*\.(js|css)?${
expires 1h;
}
error_page 500 502 503 504 /50x.html; #错误返回页面
}
}