脚本宝典收集整理的这篇文章主要介绍了Nginx:从入门到放弃,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
/
/
第一种:proxy_pass的链接无/
proxy_pass中,不带『/』,则把『匹配字符串及后缀(/api/xxx)』均带给转发地址
# 效果为:http://xxx.xxx.COM/api/xxx -> http://127.0.0.1:7000/api/xxx. 转发的时候,包含了url前缀.
location ^~ /api/ {
proxy_pass http://127.0.0.1:7000;
}
# 效果与上面一致
location ^~ /api {
proxy_pass http://127.0.0.1:7000;
}
第二种:proxy_pass的链接有/
proxy_pass中,带『/』,则把『请求地址排除匹配字符串(/api/)』后,再带给转发地址
# 效果为:http://xxx.xxx.com/api/xxx --> http://127.0.0.1:7000/xxx
location ^~ /api/ {
proxy_pass http://127.0.0.1:7000/; # 端口后多了斜杠『/』
}
# 注意:下面的代码会导致失败,原因为『/api/xxx排除了/api』后,会把『/xxx』带给转发地址,但转发地址中已有了斜杠,结果会多了一条斜杠,报错。
# 效果为:http://xxx.xxx.com/api/xxx --> http://127.0.0.1:7000//xxx
location ^~ /api { # 这里的匹配字符串最后少了斜杠『/』
proxy_pass http://127.0.0.1:7000/;
}
location的修饰符为正则匹配时,proxy_pass的地址最后不要带斜杠
Nginx的重写指令用于改变客户端的URL请求。主要有return
和rewrite
。两个指令都有重写URL的能力,但rewrite
支持更复杂的功能。
在server
中返回 301 重定向:
server {
listen 80;
server_name www.olddomain.com;
return 301 $scheme://www.newdomain.com$REQUEST_URI;
}
在 location
中返回 301 重定向:
location = /tutorial/learning-nginx {
return 301 $scheme://example.com/nginx/understanding-nginx
}
rewrite regex replacement-url [flag];
不同flag的效果:
flag | 说明 |
---|---|
last | 停止解析,并开始搜索与更改后的URI 相匹配的location ; |
break | 中止 rewrite,不再继续匹配 |
redirect | 返回临时重定向的 HTTP 状态 302 |
PErmanent | 返回永久重定向的 HTTP 状态 301 |
注意:rewrite
只能返回301和302状态码,如果需要返回其他状态码,可以在rewrite
命令后使用return
把https://example.com/nginx-tutorial
重写为https://example.com/somePage.htML
location = /nginx-tutorial
{
rewrite ^/nginx-tutorial?$ /somePage.html last;
}
把https://www.example.com/user.php?id=11
重写为https://exampleshop.com/user/11
location = /user.php
{
rewrite ^/user.php?id=([0-9]+)$ /user/$1 last;
}
其中$1
表示regex
中第一个括号中的值,第二个括号中的值可通过$2
获取
把https://www.example.com
重写为https://m.exampleshop.com
location = /
{
if ($http_user_agent ~* (mobile|nokia|iphone|ipad|andROId|samsung|htc|blackberry)) {
rewrite ^(.*) https://m.example.com$1 redirect;
}
}
location [ = | ~ | ~* | ^~ | @ ] uri { ... }
location @name { ... }
location uri { ... } // 没有修饰符
=
:精确匹配。只有请求的url路径与配置的字符串完全相等时,才会命中^~
:前缀匹配,在正则匹配前执行,且如果该项匹配上,则不再进行匹配~
:正则匹配,但是区分大小写~*
:正则匹配,不区分大小写没有修饰符
:普通匹配,当上面都匹配不上时,再进行该匹配@
:"@" 定义一个命名的 location,使用在内部定向时,例如 error_page注意:在前缀匹配和普通匹配中,按最大匹配原则进行匹配。比如在前缀匹配:location /dir01
与 location /dir01/dir02
,如有请求 http://localhost/dir01/dir02/file
将最终匹配到 location /dir01/dir02
,但请求http://localhost/dir01/dir
会匹配到location /dir01
=
)精确匹配,命中后不再匹配^~
)前缀匹配,全部匹配一次,命中最大匹配的规则,命中后不再匹配~
|~*
)正则匹配,命中第一个匹配成功的,命中后不再匹配没有修饰符
)普通匹配,全部匹配一次,命中最大匹配的规则可以自行修改下面的例子去验证,这里就不一一说明(注意:网络上一些文章描述的规则是错误的,建议自行验证)。
server {
listen 1999;
listen [::]:1999;
server_name your_host;
location /api/refresh {
rewrite ^(.*) https://www.baidu.com/s?wd=ResultH redirect;
}
location ~* .(jpg)$ {
rewrite ^(.*) https://www.baidu.com/s?wd=ResultI redirect;
}
location ~ .(gif|jpg|png|js|css)$ {
rewrite ^(.*) https://www.baidu.com/s?wd=ResultG redirect;
}
location ~* .(gif2|jpG|png|js|css)$ {
rewrite ^(.*) https://www.baidu.com/s?wd=ResultJ redirect;
}
location ^~ /api {
rewrite ^(.*) https://www.baidu.com/s?wd=ResultF redirect;
}
location = /api {
rewrite ^(.*) https://www.baidu.com/s?wd=ResultA redirect;
}
location = /LOGin {
rewrite ^(.*) https://www.baidu.com/s?wd=ResultB redirect;
}
location ^~ /api/login {
rewrite ^(.*) https://www.baidu.com/s?wd=ResultD redirect;
}
location / {
rewrite ^(.*) https://www.baidu.com/s?wd=ResultE redirect;
}
location /demo {
rewrite ^(.*) https://www.baidu.com/s?wd=ResultK redirect;
}
location /demo/run {
rewrite ^(.*) https://www.baidu.com/s?wd=ResultL redirect;
}
}
@用来定义一个命名location。主要用于内部重定向,不能用来处理正常的请求。其用法如下:
location / {
try_files $uri $uri/ @reset
}
location @reset {
# ...do something
}
注意nginx中的变量必须都以$开头
set $变量名 变量值
在不同层级的标签中声明的变量性的作用域如下:
需要修改的数据:
键 | 值 |
---|---|
server_name | 证书对应的域名 |
ssl_certificate | 证书文件的路径 |
ssl_certificate_key | 私钥文件的路径 |
server {
#SSL 访问端口号为 443
listen 443 ssl;
#填写绑定证书的域名
server_name your_hostname.com;
#证书文件名称
ssl_certificate /etc/nginx/ssl/jenkins.crt;
#私钥文件名称
ssl_certificate_key /etc/nginx/ssl/jenkins.key;
ssl_session_timeout 5m;
#请按照以下协议配置
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
ssl_ciphers ECDHE-RSA-AES128-GCM-sHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
root /VAR/www/html;
index index.html index.htm;
}
}
以上是脚本宝典为你收集整理的Nginx:从入门到放弃全部内容,希望文章能够帮你解决Nginx:从入门到放弃所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。