大家好,欢迎来到IT知识分享网。
/conf/nginx.conf为配置文件:
安装后的初始化配置为:
#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安装后默认为80端口,只有一个
listen 80;
1、使用默认端口
访问是不需要带端口的,所以可以使用ip直接访问location下的内容;
2、修改nginx端口
访问需要带上修改后的端口号: 如修改nginx端口号改成8089,localhost修改为你服务器ip地址(也可以不修改),这时候访问代理的location内容是需要带端口的,如192.168.222.129:8089。
二、http块:
只有一个,http块是Nginx服务器配置中的重要部分,代理、缓存和日志定义等绝大多数的功能和第三方模块的配置都可以放在这模块中。作用包括:文件引入、MIME-Type定义、日志自定义、是否使用sendfile传输文件、连接超时时间、单连接请求数上限等。
三、server块:
server块,虚拟主机(虚拟服务器)。作用:使得Nginx服务器可以在同一台服务器上至少运行一组Nginx进程,就可以运行多个网站。
四、location块:
location块是server块的一个指令。作用:基于Nginx服务器接收到的请求字符串,虚拟主机名称(ip,域名)、url匹配,对特定请求进行处理。一个location为一个代理,即一个请求地址。
1、 location语法规则
location [=|~|~*|^~] /uri/ { … }
1.1、= 开头
表示精确匹配
1.2、^~ 开头
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。以xx开头
1.3、~ 开头
表示区分大小写的正则匹配 以xx结尾
1.4、~* 开头
表示不区分大小写的正则匹配 以xx结尾
1.5、!~和!~*
分别为区分大小写不匹配及不区分大小写不匹配 的正则
1.6、/
通用匹配,任何请求都会匹配到。
2、匹配优先级:
精确匹配 =-》其次以xx开头匹配^~-》然后是按文件中顺序的正则匹配-》最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
3、demo:
location = / {
#规则A
}
location = /login {
#规则B
}
location ^~ /static/ {
#规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
#规则D,注意:是根据括号内的大小写进行匹配。括号内全是小写,只匹配小写
}
location ~* \.png$ {
#规则E
}
location !~ \.xhtml$ {
#规则F
}
location !~* \.xhtml$ {
#规则G
}
location / {
#规则H
}
那么产生的效果如下:
访问根目录/, 比如http://localhost/ 将匹配规则A
访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
访问 http://localhost/static/a.html 将匹配规则C
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用, 而 http://localhost/static/c.png 则优先匹配到 规则C
访问 http://localhost/a.PNG 则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。
访问 http://localhost/a.xhtml 不会匹配规则F和规则G,
http://localhost/a.XHTML不会匹配规则G,(因为!)。规则F,规则G属于排除法,符合匹配规则也不会匹配到,所以想想看实际应用中哪里会用到。
访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。
五、location下root和alias区别:
1、root属性:
实际代理的路径 = root代理的路径 + location的路径 。如
location /test/{
root /html/static/;
}
访问http://localhost/test/abc.html , 实际访问服务器目录为: /html/static/test/abc.html
#代理vue页面为nginx首页
location / {
root /data/public/nginx/html/dist/dist; #路由写的不对 容易导致 500 internal错误
index index.html index.htm;
try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
}
2、alias属性:
实际代理的路径 = alias代理的路径。使用alias时,目录名后面一定要加/。 alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。 alias只能位于location块中(root可以不放在location中)。如
location /test/{
ailas /html/static/;
}
访问http://localhost/test/abc.html , 实际访问服务器目录为: /html/static/abc.html
六、location下index:
在匹配了一个 location 块后,如果这个块有或者继承了 index 指令,就会触发一次内部重定向,也就是重新开始一次 location search, 然后匹配到了第二个 location block。
七、location下proxy_pass:
Nginx的官网将proxy_pass分为两种类型:
1、不带URI方式:
只包含IP和端口号的(连端口之后的/也没有),如proxy_pass http://localhost:8080。这种方式,nginx将会保留location中的路径部分,如:
location /api1/ { proxy_pass http://localhost:8080; }
在访问http://localhost/api1/xxx时,会代理到http://localhost:8080/api1/xxx
2、带url方式:
在端口号之后有其他路径的,包含了只有单个/的如proxy_pass http://localhost:8080/,以及其他路径比如proxy_pass http://localhost:8080/abc,这两种路径的匹配方式是一样的。这种方式nginx将使用诸如alias的替换方式对URL进行替换,并且这种替换只是字面上的替换。举例说明:
1)单个端口号后加/的:比如:
location /api2/ { proxy_pass http://localhost:8080/; }
当访问http://localhost/api2/xxx时,http://localhost/api2/(注意最后的/)被替换成了http://localhost:8080/,然后再加上剩下的xxx,于是变成了http://localhost:8080/xxx。
2)端口号后面还有其他路径的:比如:
location /api5/ { proxy_pass http://localhost:8080/haha; }
当访问http://localhost/api5/xxx时,http://localhost/api5/被替换成了http://localhost:8080/haha,请注意这里haha后面没有/,然后再加上剩下的xxx,即http://localhost:8080/haha+xxx=http://localhost:8080/hahaxxx。
总结如下:
server { listen 80; server_name localhost; location /api1/ { proxy_pass http://localhost:8080; } # http://localhost/api1/xxx -> http://localhost:8080/api1/xxx location /api2/ { proxy_pass http://localhost:8080/; } # http://localhost/api2/xxx -> http://localhost:8080/xxx location /api3 { proxy_pass http://localhost:8080; } # http://localhost/api3/xxx -> http://localhost:8080/api3/xxx location /api4 { proxy_pass http://localhost:8080/; } # http://localhost/api4/xxx -> http://localhost:8080//xxx,请注意这里的双斜线,好好分析一下。 location /api5/ { proxy_pass http://localhost:8080/haha; } # http://localhost/api5/xxx -> http://localhost:8080/hahaxxx,请注意这里的haha和xxx之间没有斜杠,分析一下原因。 location /api6/ { proxy_pass http://localhost:8080/haha/; } # http://localhost/api6/xxx -> http://localhost:8080/haha/xxx location /api7 { proxy_pass http://localhost:8080/haha; } # http://localhost/api7/xxx -> http://localhost:8080/haha/xxx location /api8 { proxy_pass http://localhost:8080/haha/; } # http://localhost/api8/xxx -> http://localhost:8080/haha//xxx,请注意这里的双斜杠。 }
八、upstream:
可以对location实现负载均衡。
1、语法:
upstream中server指令语法如下:
server address [parameters]
举例:
upstream backend { server backend1.example.com weight=5; server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; }
用法举例:
upstream mydemoservice{
server localhost:7777 weight=5;
server localhost:9999 weight=5;
}
server {
listen 80;
server_name localhost;
#访问方式:http://localhost/demo/code/getCode,代理到http://localhost:9999/demo/code/getCode接口
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost:9999;
}
location /myserver/ {
proxy_pass http://mydemoservice/;
}
location /xxl-job-admin/ { proxy_pass http://xxljob; 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_set_header X-Forwarded-Proto $scheme; }
2、分配方式:
Nginx的upstream支持5种分配方式,下面将会详细介绍,其中,前三种为Nginx原生支持的分配方式,后两种为第三方支持的分配方式:
2.1、轮询:
轮询是upstream的默认分配方式,即每个请求按照时间顺序轮流分配到不同的后端服务器,如果某个后端服务器down掉后,能自动剔除
upstream backend { server 192.168.1.101:8888; server 192.168.1.102:8888; server 192.168.1.103:8888; }
2.2、weight :
轮询的加强版,即可以指定轮询比率,weight和访问几率成正比,主要应用于后端服务器异质的场景下。注意weight和=中间不要有空格。
upstream backend { server 192.168.1.101 weight=1; server 192.168.1.102 weight=2; server 192.168.1.103 weight=3; }
2.3、ip_hash :
每个请求按照访问ip(即Nginx的前置服务器或者客户端IP)的hash结果分配,这样每个访客会固定访问一个后端服务器,可以解决session一致问题。
upstream backend { ip_hash; server 192.168.1.101:7777; server 192.168.1.102:8888; server 192.168.1.103:9999; }
2.4、fair:
fair顾名思义,公平地按照后端服务器的响应时间(rt)来分配请求,响应时间短即rt小的后端服务器优先分配请求。
upstream backend { server 192.168.1.101; server 192.168.1.102; server 192.168.1.103; fair; }
2.5、url_hash:
与ip_hash类似,但是按照访问url的hash结果来分配请求,使得每个url定向到同一个后端服务器,主要应用于后端服务器为缓存时的场景下。
upstream backend { server 192.168.1.101; server 192.168.1.102; server 192.168.1.103; hash $request_uri; hash_method crc32; }
其中,hash_method为使用的hash算法,需要注意的是:此时,server语句中不能加weight等参数。
3、demo:
1)
location /router/ { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real_IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
2)以下可以解决只有一个合法的业务域名的问题
location /test/router { proxy_pass http://test.yl.66huyu.cn/router; proxy_set_header Host $host; proxy_set_header X-Real_IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
3)做游戏多区多服的https的反向代理设置:
location /zone { 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_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 86400; proxy_pass http://$arg_host:$arg_port; }
客户端请求 :wss://www.ooxx.com/zone?host=134.175.33.159&port=8888
nginx自动会把参数加arg_前缀,代理向ttp://$arg_host:$arg_port,则客户端自动就连向了http://134.175.33.151:8888
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/109807.html
