首页 > 系统相关 >nginx rewrite模块之if和return

nginx rewrite模块之if和return

时间:2024-08-29 09:16:48浏览次数:12  
标签:return ssl rewrite apps magedu nginx4 access nginx org

ngx_http_rewrite_module

将用户请求的URI基于PCRE regex所描述的模式进行检查,而后完成重定向替换  

if:
Syntax: if (condition) { ... }
  Default: —
  Context: server, location
  条件满足时,执行配置块中的配置指令
condition:
  比较操作符:
    = 相同 != 不同
    ~ 模式匹配,区分字符大小写
    ~* 模式匹配,不区分字符大小写
    !~ 模式不匹配,区分字符大小写
    !~* 模式不匹配,不区分字符大小写
  文件及目录存在性判断:
    -e,!-e 存在与否(包括文件,目录,软链接)
    -f,!-f 文件
    -d,!-d 目录
    -x,!-x 执行

 

注意: if (condition) { ... } 语句中,如果$变量的值为空字符串或是以0开头的任意字符串,则 if 指令认为该条件为false,其它条件为true

 

return:
  return code [text]; #返回客户端指定的状态码和文本说明
  return code URL;
  return URL;

停止处理,并返回给客户端指定的响应码(包括: 204, 400, 402 — 406, 408, 410, 411, 413, 416, 500 — 504),
对 301, 302, 303, 307, 308跳转到URL
  在版本1.1.16和1.0.13之前,代码307没有被视为重定向
  在版本1.13.0之前,代码308未被视为重定向
Default: —
Context: server, location, if

示例:return实现URL重写

[[email protected] certs]# vim /apps/nginx4/conf/conf.d/test.conf 

server {
        listen 443 ssl;
        listen 80;
        server_name www.magedu.org;
        root /data/site14/;
        #ssl on;
        ssl_certificate /apps/nginx4/ssl/magedu.org.crt;
        ssl_certificate_key /apps/nginx4/ssl/magedu.org.key;
        ssl_session_cache shared:sslcache:20m;
        ssl_session_timeout 10m;
        access_log /apps/nginx4/logs/magedu.org.ssl.access.log  access_json ;
        location / {
                if ( $scheme = http ){
                        return 301 https://www.magedu.org;      #条件判断重定向
                }
        }
}
#server {

#       server_name www.magedu.org;
#       root /data/site14/;
#       access_log /apps/nginx4/logs/magedu.org.access.log  access_json ;
#       default_type text/html ;
#}

验证:

[[email protected] certs]# curl -I http://www.magedu.org/
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Sun, 07 Mar 2021 07:16:56 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://www.magedu.org

[[email protected] certs]# curl -Lk http://www.magedu.org/
/data/site14/index.html

示例:拒绝curl访问

配置:
[[email protected] certs]# vim /apps/nginx4/conf/conf.d/test.conf server { listen 443 ssl; listen 80; server_name www.magedu.org; root /data/site14/; #ssl on; ssl_certificate /apps/nginx4/ssl/magedu.org.crt; ssl_certificate_key /apps/nginx4/ssl/magedu.org.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; access_log /apps/nginx4/logs/magedu.org.ssl.access.log access_json ; location / { if ($http_user_agent ~* curl) { return 301; } } }
验证: [[email protected] certs]# curl http://www.magedu.org <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.14.2</center> </body> </html>

 

配置:
[[email protected] certs]# vim /apps/nginx4/conf/conf.d/test.conf server { listen 443 ssl; listen 80; server_name www.magedu.org; root /data/site14/; #ssl on; ssl_certificate /apps/nginx4/ssl/magedu.org.crt; ssl_certificate_key /apps/nginx4/ssl/magedu.org.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; access_log /apps/nginx4/logs/magedu.org.ssl.access.log access_json ; location / { if ($http_user_agent ~* curl) { return 444; } } }
验证:停止处理并将指定的代码返回给客户端。非标准代码444在不发送响应报头的情况下关闭连接 [[email protected] certs]# curl http://www.magedu.org curl: (52) Empty reply from server

 

配置:
[[email protected] certs]# vim /apps/nginx4/conf/conf.d/test.conf server { listen 443 ssl; listen 80; server_name www.magedu.org; root /data/site14/; #ssl on; ssl_certificate /apps/nginx4/ssl/magedu.org.crt; ssl_certificate_key /apps/nginx4/ssl/magedu.org.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; access_log /apps/nginx4/logs/magedu.org.ssl.access.log access_json ; location / { if ($http_user_agent ~* curl) { return 405 ; } } }
验证: [[email protected] certs]# curl http://www.magedu.org <html> <head><title>405 Not Allowed</title></head> <body bgcolor="white"> <center><h1>405 Not Allowed</h1></center> <hr><center>nginx/1.14.2</center> </body> </html>

 

配置:临时跳转
[[email protected] certs]# vim /apps/nginx4/conf/conf.d/test.conf server { listen 443 ssl; listen 80; server_name www.magedu.org; root /data/site14/; #ssl on; ssl_certificate /apps/nginx4/ssl/magedu.org.crt; ssl_certificate_key /apps/nginx4/ssl/magedu.org.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; access_log /apps/nginx4/logs/magedu.org.ssl.access.log access_json ; location / { if ($http_user_agent ~* curl) { return http://www.magedu.com ; #curl的结果默认302,临时跳转 } } } 配置:永久跳转 [[email protected] certs]# vim /apps/nginx4/conf/conf.d/test.conf server { listen 443 ssl; listen 80; server_name www.magedu.org; root /data/site14/; #ssl on; ssl_certificate /apps/nginx4/ssl/magedu.org.crt; ssl_certificate_key /apps/nginx4/ssl/magedu.org.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; access_log /apps/nginx4/logs/magedu.org.ssl.access.log access_json ; location / { if ($http_user_agent ~* curl) { return 301 http://www.magedu.com ; #curl的结果301,永久跳转 } } }

 

 

 

标签:return,ssl,rewrite,apps,magedu,nginx4,access,nginx,org
From: https://www.cnblogs.com/cnblogsfc/p/14591546.html

相关文章

  • nginx之http跳转https
    http跳转https实现示例:ssl配置参考“https”实现文章法一:2个虚拟主机配置:[[email protected]]#vim/apps/nginx4/conf/conf.d/test.confserver{listen443ssl;#listen80;server_namewww.magedu.org;root/data/site14/;......
  • nginx rewrite模块之rewrite
    未完成ngx_http_rewrite_module rewriteregexreplacement[flag]将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为replacement指定的新的URI注意:如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会重新一轮......
  • nginx之错误url重定向到首页
    nginx之错误url重定向到首页1、配置:[[email protected]~]#vitest.confserver{listen443ssl;listen80;server_namewww.magedu.org;root/data/site14/;#sslon;ssl_certificate/apps/nginx4/ssl/magedu.org.crt;......
  • nginx扩展之支持多个ssl加密虚拟主机
    nginx支持一台服务器唯一IP:PORT,根据server_name创建区分多个经过ssl加密的https虚拟主机,apache不支持 生成www.magedu.net域名证书:[[email protected]]#cd/etc/pki/tls/certs/[[email protected]]#vimMakefile%.key:umask77;\#/usr/bin/ope......
  • nginx共享资源站点
    配置nginx文件下载服务模块:ngx_http_autoindex_module常用指令:autoindexon|off;自动文件索引功能,默为offDefault: autoindexoff;Context: http,server,locationautoindex_exact_sizeon|off;计算文件确切大小(单位bytes),off显示大概大小(单位K、M),默......
  • nginx基于用户的访问控制
    ngx_http_auth_basic_module模块 实现基于用户的访问控制,使用basic机制进行用户认证auth_basicstring|off;auth_basic_user_filefile;Default: —Context: http,server,location,limit_exceptlocation/admin/{ auth_basic"AdminArea"......
  • nginx日志格式
    ngx_http_log_modulengx_http_log_module模块指定日志格式记录请求log_formatnamestring...;string可以使用nginx核心模块及其它模块内嵌的变量Default:log_formatcombined"...";Context: httpaccess_logpath[format[buffer=size][gzip[=level]......
  • nginx第三方模块echo和变量
    第三方模块:echo第三模块是对nginx的功能扩展,第三方模块需要在编译安装nginx的时候使用参数--add-module=PATH指定路径添加,有的模块是由公司的开发人员针对业务需求定制开发的,有的模块是开源爱好者开发好之后上传到github进行开源的模块,nginx支持第三方模块,需要重新编译源码......
  • nginx输出nginx的基本状态信息
    输出nginx的基本状态信息模块:ngx_http_stub_status_module输出信息示例:Activeconnections:291server   accepts  handled  requests#下面三个数分别对应accepts,handled,requests      166309481663094831070465Reading:6Writing:179Wait......
  • nginx 压缩gzip
    ngx_http_gzip_modulengx_http_gzip_module用gzip方法压缩响应数据,节约带宽gzipon|off;启用或禁用gzip压缩Default: gzipoff;Context: http,server,location,ifinlocationgzip_comp_levellevel;消耗CPU 压缩比由低到高:1到9,默认:1 gzip_di......