#Nginx所用用户和组,window下不指定
#user nobody;
#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 1;
#全局错误日志定义类型[ debug | info | notice | warn | error | crit ]
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#指定pid存放文件 属进程文件
#pid logs/nginx.pid;
#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
#worker_rlimit_nofile 65535;
#工作模式与连接数上限
events {
#使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
#epoll模型是linux2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
#use epoll;
worker_connections 1024;#单个进程最大连接数(最大连接数=连接数*进程数)
}
#设定http服务器
http {
include mime.types;#文件扩展名与文件类型映射表
default_type application/octet-stream;#默认文件类型
#charset utf-8; #默认编码
##server_names_hash_bucket_size 128; #服务器名字的hash表大小
##client_header_buffer_size 32k; #上传文件大小限制
##large_client_header_buffers 4 64k; #设定请求缓
##client_max_body_size 8m; #设定请求缓
#定义日志格式
#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;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改 成off。
##autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
##tcp_nopush on;#防止网络阻塞
##tcp_nodelay on; #防止网络阻塞
#keepalive_timeout 0;
keepalive_timeout 65;#长连接超时时间,单位是秒
##相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
##fastcgi_connect_timeout 300;
##fastcgi_send_timeout 300;
#f#astcgi_read_timeout 300;
##fastcgi_buffer_size 64k;
##fastcgi_buffers 4 64k;
##fastcgi_busy_buffers_size 128k;
##fastcgi_temp_file_write_size 128k;
##gzip模块设置
##include gzip.conf;
##gzip on; #开启gzip压缩输出
##gzip_min_length 1k; #最小压缩文件大小
##gzip_buffers 4 16k; #压缩缓冲区
##gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
##gzip_comp_level 2; #压缩等级
##gzip_types text/plain application/x-javascript text/css application/xml;
##压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
##gzip_vary on;
##limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用
#集群中的所有后台服务器的配置信息
#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weight参数表示权值,权值越高被分配到的几率越大。
upstream tomcats{
#同一机器在多网情况下,路由切换,ip可能不同
#ip_hash;
server 127.0.0.1:8080;
server 127.0.0.1:9090;
}
#server相当于一个代理服务器,当然可以配置多个
#也可当作是虚拟主机的配置
server {
listen 80;#HTTP的端口,表示当前的代理服务器监听的端口,默认的是监听80端口。注意,如果我们配置了多个server,这个listen要配置不一样,不然就不能确定转到哪里去了。
server_name aaa;#域名可以有多个,用空格隔开
#charset koi8-r;
#access_log logs/host.access.log main;
##设定查看Nginx状态的地址
location ~ ^/NginxStatus/ {
stub_status on; #Nginx 状态监控配置
access_log off;
##auth_basic "NginxStatus";
##auth_basic_user_file conf/htpasswd;
}
#end 增加内容
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcats;
proxy_set_header X-Real-IP $remote_addr;#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
##以下是一些反向代理的配置,可选。
##proxy_set_header Host $host;
##client_max_body_size 10m; #允许客户端请求的最大单文件字节数
##client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 30;#nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 30;#后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 30;#连接成功后,后端服务器响应时间(代理接收超时)
##proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
##proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置
##proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
##proxy_temp_file_write_size 64k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传
}
#本地动静分离反向代理配置
#所有jsp的页面均交由tomcat或resin处理
#location ~ .(jsp|jspx|do)?$ {
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_pass http://127.0.0.1:8080;
#}
#所有静态文件由nginx直接读取不经过tomcat或resin
#location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
#{ expires 15d; }
#location ~ .*.(js|css)?$
#{ expires 1h; }
#}
#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;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
windows:
停止:nginx -s stop
检查:nginx -t
nginx -s quit 安全退出
nginx -s stop 强制退出
nginx -s reload 修改配置文件后,重启nginx使配置文件生效
不用记,nginx -?就都有了
nginx是个代理服务器,它把浏览器过来的请求先经过它转发给实际服务器
nginx可以做url重写
nginx可以动静分离
nginx可做集群
匹配规则:
location匹配命令
~ #波浪线表示执行一个正则匹配,区分大小写
~* #表示执行一个正则匹配,不区分大小写
^~ #^~表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
= #进行普通字符精确匹配
@ #"@" 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
location 匹配的优先级(与location在配置文件中的顺序无关)
= 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。
普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。
最后匹配理带有"~"和"~*"的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。
location 优先级官方文档
- Directives with the = prefix that match the query exactly. If found, searching stops.
- All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
- Regular expressions, in order of definition in the configuration file.
- If #3 yielded a match, that result is used. Else the match from #2 is used.
- =前缀的指令严格匹配这个查询。如果找到,停止搜索。
- 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。
- 正则表达式,在配置文件中定义的顺序。
- 如果第3条规则产生匹配的话,结果被使用。否则,如同从第2条规则被使用。
例如
location = / {
# 只匹配"/".
[ configuration A ]
}
location / {
# 匹配任何请求,因为所有请求都是以"/"开始
# 但是更长字符匹配或者正则表达式匹配会优先匹配
[ configuration B ]
}
location ^~ /images/ {
# 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
[ configuration C ]
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配以 gif, jpg, or jpeg结尾的请求.
# 但是所有 /images/ 目录的请求将由 [Configuration C]处理.
[ configuration D ]
}
请求URI例子:
- / -> 符合configuration A
- /documents/document.html -> 符合configuration B
- /images/1.gif -> 符合configuration C
- /documents/1.jpg ->符合 configuration D
@location 例子
error_page 404 = @fetch;
location @fetch(
proxy_pass http://fetch;
)
我监听的端口改成81,登陆之后重定向直接定向到80端口了,在配置文件中在$host后边加上81端口就好了。