1.配置文件结构
由四部分组成:全局配置、events配置、http配置、server配置、location配置。server配置在http配置里面。
全局配置:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
events配置:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
http配置:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
server配置:配置虚拟主机的相关参数,一个http中可以有多个server。
location配置:配置请求的路由,以及各种页面的处理情况。
惊群现象:一个网路连接到来,多个睡眠的进程被同事叫醒,但只有一个进程能获得链接,这样会影响系统性能。
每个指令必须有分号结束。
####################################### 全局配置
…
####################################### events配置
events { #events块
…
}
####################################### http配置
http
{
… #http全局块
####################################### server配置
server
{
… #server全局块
location [PATTERN] #location块
{
…
}
location [PATTERN]
{
…
}
}
server
{
…
}
… #http全局块
}#######################################全局配置
#nginx用户
#user administrator administrators; #配置用户或者组,默认为nobody nobody。
#允许生成的进程数,默认为1,由cpu核心总数决定最大值,auto 在 nginx 1.3.8 和 1.2.5 之后的版本开始支持。
worker_processes 1;
# 日志路径和级别。可放在全局、http、server配置中,级别有:debug|info|notice|warn|error|crit|alert|emerg
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#PID文件位置(pid为进程id号)
#pid logs/nginx.pid;
####################################### events 配置
#工作模式
events {
#设置网路连接序列化,防止惊群现象发生,默认为on
accept_mutex on;
#设置一个进程是否同时接受多个网络连接,默认为off
multi_accept on;
#事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
#use epoll;
#每个进程的最大连接数,默认1024;(如每个进程连接数1000,有两个进程数。则可以提供2000个连接)
worker_connections 1024;
}
####################################### http 配置
#http相关与虚拟主机配置
http {
#文件扩展名与文件类型映射表,类型在文件 mime.types中定义
include mime.types;
include conf.d/*.conf;
#请求默认文件类型,默认为text/plain
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函数输出日志,可在http、server、location配置中
sendfile on;
#每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
sendfile_max_chunk 100k;
#tcp_nopush on;
#连接超时时间
keepalive_timeout 65;
#开启gzip压缩,节约带宽。
#gzip on;
# 上游机,为代理或者负载均衡做准备
upstream server_test {
#加ip_hash后当前用户的请求只能去同一个服务。
#ip_hash;
#down 表示当前的server暂时不参与负载
#weight 权重
#max_fails 请求最大失败次数,默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
#fail_timeout 当达到max_fails次数后,服务暂停时间。
#backup: 热备、后备服务器,当其它非backup机器down或者忙的时,请求至backup机器。
server 10.0.0.11:9090 down;
server 127.0.0.1:1111 weight=4 max_fails=3 fail_timeout=0s;
server 127.0.0.1:8089 weight=1 max_fails=3 fail_timeout=0s;
server 127.0.0.1:8088 backup;
}
####################################### server 配置
server {
#单连接请求上限次数。
keepalive_requests 120;
#监听端口
listen 80;
#监听地址
server_name www.vue.com 127.0.0.1;
charset utf-8;
#access_log logs/host.access.log main;
#正常普通网页解析
location / {
root /git/antd-demo/dist;
#默认页
index index.html;
#拒绝的ip
#deny 127.0.0.1;
#允许的ip
#allow 172.18.5.54;
}
#2图片解析
location /images/ {
root G:/web;
#开启图片浏览功能
autoindex on;
}
#3反向代理
location ^~ /test/ {
#server_test对应upstream server_test
proxy_pass http://server_test/;
#连接时间超时时间设置
#proxy_connect_timeout 1;
#proxy_read_timeout 1;
#proxy_send_timeout 1;
}
#4静态资源解析
location ~ .*\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt)$ {
root web/static;
}
error_page 404 /404.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;
#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;
#
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;
#}
}
}2.listen
配置监听的IP地址
listen address[:port] [default_server] [setfib=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [deferred] [accept_filter=filter] [bind] [ssl];
配置监听端口
listen port[default_server] [setfib=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [ssl];
配置 UNIX Domain Socket
listen unix:path [default_server] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ssl];listen *:80 | *:8080 #监听所有80端口和8080端口
listen IP_address:port #监听指定的地址和端口号
listen IP_address #监听指定ip地址所有端口
listen port #监听该端口的所有IP连接
path:socket文件路径,如 var/run/nginx.sock等。
default_server:标识符,将此虚拟主机设置为 address:port 的默认主机。(在 nginx-0.8.21 之前使用的是 default 指令)
setfib=number:Nginx-0.8.44 中使用这个变量监听 socket 关联路由表,目前只对 FreeBSD 起作用,不常用。
backlog=number:设置监听函数listen()最多允许多少网络连接同时处于挂起状态,在 FreeBSD 中默认为 -1,其他平台默认为511.
rcvbuf=size:设置监听socket接收缓存区大小。
sndbuf=size:设置监听socket发送缓存区大小。
deferred:标识符,将accept()设置为Deferred模式。
accept_filter=filter:设置监听端口对所有请求进行过滤,被过滤的内容不能被接收和处理,本指令只在 FreeBSD 和 NetBSD 5.0+ 平台下有效。filter 可以设置为 dataready 或 httpready 。
bind:标识符,使用独立的bind() 处理此address:port,一般情况下,对于端口相同而IP地址不同的多个连接,Nginx 服务器将只使用一个监听指令,并使用 bind() 处理端口相同的所有连接。
sl:标识符,设置会话连接使用 SSL模式进行,此标识符和Nginx服务器提供的 HTTPS 服务有关。3.location
3.1 请求地址格式
= :严格匹配,用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配 成功,就停止继续向下搜索并立即处理该请求。默认严格匹配。
~:区分大小写的正则表达式
~*:不区分大小写的正则表达式
^~:取反的区分大小写的正则表达式
3.2 root 指令
将搜索的根设置为 root 设定的目录,即不会截断 uri,而是使用原始 uri 跳转该目录下查找文件
示例 1:
#------------目录结构----------
/www/x1/index.html
/www/x2/index.html
#--------配置-----------------------
index index.html index.php;
location /x/ {
root "/www/";
}
#-------访问--------------
curl http://localhost/x1/index.html
curl http://localhost/x2/index.html
3.3 alias
会截断匹配的 uri,然后使用 alias 设定的路径加上剩余的 uri 作为子路径进行查找。
示例 2:
#----------配置-----------------
location /y/z/ {
alias /www/x1/;
}
#---------访问--------------
curl http://localhost/y/z/index.html
3.4 proxy_pass指令
该指令用于设置被代理服务器的地址。可以是主机名称、IP地址加端口号的形式。
proxy_pass URL;
URL 为被代理服务器的地址,可以包含传输协议、主机名称或IP地址加端口号,URI等。
如果 proxy_pass 的 url 不带 uri
如果尾部是"/",则会截断匹配的uri
如果尾部不是"/",则不会截断匹配的uri
如果proxy_pass的url带uri,则会截断匹配的uri
示例:
#-------servers配置--------------------
location / {
echo $uri #回显请求的uri
}
#--------proxy_pass配置---------------------
location /t1/ { proxy_pass http://servers; } #正常,不截断
location /t2/ { proxy_pass http://servers/; } #正常,截断
location /t3 { proxy_pass http://servers; } #正常,不截断
location /t4 { proxy_pass http://servers/; } #正常,截断
location /t5/ { proxy_pass http://servers/test/; } #正常,截断
location /t6/ { proxy_pass http://servers/test; } #缺"/",截断
location /t7 { proxy_pass http://servers/test/; } #含"//",截断
location /t8 { proxy_pass http://servers/test; } #正常,截断
#---------访问----------------------
for i in $(seq 6)
do
url=http://localhost/t$i/doc/index.html
echo "-----------$url-----------"
curl url
done
#--------结果---------------------------
----------http://localhost:8080/t1/doc/index.html------------
/t1/doc/index.html
----------http://localhost:8080/t2/doc/index.html------------
/doc/index.html
----------http://localhost:8080/t3/doc/index.html------------
/t3/doc/index.html
----------http://localhost:8080/t4/doc/index.html------------
/doc/index.html
----------http://localhost:8080/t5/doc/index.html------------
/test/doc/index.html
----------http://localhost:8080/t6/doc/index.html------------
/testdoc/index.html
----------http://localhost:8080/t7/doc/index.html------------
/test//doc/index.html
----------http://localhost:8080/t8/doc/index.html------------
/test/doc/index.html
4.server_name
该指令用于虚拟主机的配置
4.1基于名称的虚拟主机配置
server_name name …;
对于name 来说,可以只有一个名称,也可以有多个名称,中间用空格隔开。而每个名字由两段或者三段组成,每段之间用“.”隔开。
server_name 123.com www.123.com
使用通配符“*”,但通配符只能用在由三段字符组成的首段或者尾端,或者由两端字符组成的尾端。
server_name .123.com www.123.
使用正则表达式,用“~”作为正则表达式字符串的开始标记。
server_name ~^www\d+.123.com$;
4.2基于IP地址的虚拟主机配置
语法结构和基于域名匹配一样,而且不需要考虑通配符和正则表达式的问题。
server_name 192.168.1.
4.3基于端口的虚拟主机配置
listen 80 88
5.配置nginx的linux内核优化
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
#以下参数是对iptables防火墙的优化,防火墙不开会提示,可以忽略不理。
net.ipv4.ip_conntrack_max = 25000000
net.ipv4.netfilter.ip_conntrack_max=25000000
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established=180
net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait=120
net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait=60
net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait=120
6.nginx全局变量
$args:这个变量等于请求行中的参数,同$query_string。
$is_args: 如果已经设置$args,则该变量的值为"?",否则为""。
$content_length: 请求头中的Content-length字段。
$content_type: 请求头中的Content-Type字段。
$document_uri: 与$uri相同。
$document_root: 当前请求在root指令中指定的值。
$host: 请求主机头字段,否则为服务器名称。
$http_user_agent: 客户端agent信息。
$http_cookie: 客户端cookie信息。
$limit_rate: 这个变量可以限制连接速率。
$request_method: 客户端请求的动作,通常为GET或POST。
$remote_addr: 客户端的IP地址。
$remote_port: 客户端的端口。
$remote_user: 已经经过Auth Basic Module验证的用户名。
$request_body_file`: 客户端请求主体的临时文件名。
$request_uri: 请求的URI,带参数
$request_filename: 当前请求的文件路径,由root或alias指令与URI请求生成。
$scheme: 所用的协议,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect;。
$server_protocol: 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr: 服务器地址,在完成一次系统调用后可以确定这个值。
$server_name: 服务器名称。
$server_port: 请求到达服务器的端口号。
$request_uri: 包含请求参数的原始URI,不包含主机名,如:/foo/bar.php?arg=baz,它无法修改。
$uri: 不带请求参数的当前URI,$uri不包含主机名,如/foo/bar.html可能和最初的值有不同,比如经过重定向之类的。它可以通过内部重定向,或者使用index指令进行修改。不包括协议和主机名,例如/foo/bar.html。
$time_local : 用来记录访问时间与时区;
$status : 用来记录请求状态;成功是200;
$body_bytes_s ent :记录发送给客户端文件主体内容大小;
$http_referer :用来记录从那个页面链接访问过来的;
$http_user_agent :记录客户端浏览器的相关信息;
访问链接是:http://localhost:88/test1/test.php
网站路径是:/var/www/html
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test.php
$document_uri:/test1/test.php
$document_root:/var/www/html
$request_filename:/var/www/html/test1/test.php
7.index
该指令用于设置网站的默认首页。
index filename …;
后面的文件名称可以有多个,中间用空格隔开。
index index.html index.jsp;
index /html/index.html /php/index.php;
通常该指令有两个作用:第一个是用户在请求访问网站时,请求地址可以不写首页名称;第二个是可以对一个请求,根据请求内容而设置不同的首页。
7.错误页面
error_page 404 /404.html;
error_page 502 503 /50x.html;
error_page 404 =200 /1x1.gif;
location / {
error_page 404 @fallback;
}
location @fallback {
# 将请求反向代理到上游服务器处理
proxy_pass http://localhost:9000;
}
8.try_files
try_files $uri $uri.html $uri/index.html @other;
location @other {
# 尝试寻找匹配 uri 的文件,失败了就会转到上游处理
proxy_pass http://localhost:9000;
}
location / {
# 尝试寻找匹配 uri 的文件,没找到直接返回 502
try_files $uri $uri.html =502;
}
————Blueicex 2020/2/25 09:58 blueice1980@126.com