Nginx:从入门到放弃

发布时间:2022-07-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Nginx:从入门到放弃脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

地址转发(Proxy_pass)

两种情况

主要分为两种情况,以及一些注意事项:@H_126_5@

  • PRoxy_pass的链接无/
  • proxy_pass的链接有/

第一种: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的地址最后不要带斜杠

地址重写(return和rewrITe)

Nginx的重写指令用于改变客户端的URL请求。主要有returnrewrite。两个指令都有重写URL的能力,但rewrite支持更复杂的功能。

Return指令

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指令

语法介绍

rewrite regex replacement-url [flag];
  • regex: 正则表达式
  • replacement-url: 替换的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匹配规则

语法规则

location [ = | ~ | ~* | ^~ | @ ] uri { ... }
location @name { ... }
location uri { ... }  // 没有修饰符
  • = :精确匹配。只有请求的url路径与配置的字符串完全相等时,才会命中
  • ^~ :前缀匹配,在正则匹配前执行,且如果该项匹配上,则不再进行匹配
  • ~:正则匹配,但是区分大小写
  • ~* :正则匹配,不区分大小写
  • 没有修饰符:普通匹配,当上面都匹配不上时,再进行该匹配
  • @ :"@" 定义一个命名的 location,使用在内部定向时,例如 error_page

注意:在前缀匹配和普通匹配中,按最大匹配原则进行匹配。比如在前缀匹配:location /dir01location /dir01/dir02,如有请求 http://localhost/dir01/dir02/file 将最终匹配到 location /dir01/dir02,但请求http://localhost/dir01/dir会匹配到location /dir01

匹配规则

  1. 先进行(=)精确匹配,命中后不再匹配
  2. 再进行(^~)前缀匹配,全部匹配一次,命中最大匹配的规则,命中后不再匹配
  3. 再进行(~|~*)正则匹配,命中第一个匹配成功的,命中后不再匹配
  4. 进行(没有修饰符)普通匹配,全部匹配一次,命中最大匹配的规则
  5. 以上规则都没有命中,则报错404

例子

可以自行修改下面的例子去验证,这里就不一一说明(注意:网络上一些文章描述的规则是错误的,建议自行验证)。

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 @name的用法

@用来定义一个命名location。主要用于内部重定向,不能用来处理正常的请求。其用法如下:

location / {
    try_files $uri $uri/ @reset
}
location @reset {
    # ...do something
}

变量

自定义

注意nginx中的变量必须都以$开头

set $变量名 变量值

变量作用域

在不同层级的标签中声明的变量性的作用域如下:

  1. location标签中声明的变量中对这个location块可见
  2. server标签中声明的变量对server块以及server块中的所有子块可见

配置Https

需要修改的数据:

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,请注明来意。