nginx的set指令
set 指令
set 指令是用于定义一个变量,并且赋值
应用环境:
server,location,if
应用示例
例8:
#http://alice.testpm.com ==> http://www.testpm.com/alice
#http://jack.testpm.com ==> http://www.testpm.com/jack
[root@nginx-server conf.d]# cd /usr/share/nginx/html/
[root@nginx-server html]# mkdir jack alice
[root@nginx-server html]# echo "jack.." >> jack/index.html
[root@nginx-server html]# echo "alice.." >> alice/index.html
a. DNS实现泛解析
* IN A 网站IP
或者本地解析域名host文件
10.0.105.202 www.testpm.com
10.0.105.202 alice.testpm.com
10.0.105.202 jack.testpm.com
编辑配置文件:
server {listen 80;server_name www.testpm.com;
location / {root /usr/share/nginx/html;index index.html index.htm;if ( $host ~* ^www.testpm.com$) {break;}if ( $host ~* "^(.*)\.testpm\.com$" ) {set $user $1;rewrite .* http://www.testpm.com/$user permanent;}}location /jack {root /usr/share/nginx/html;index index.html index.hml;}location /alice {root /usr/share/nginx/html;index index.html index.hml;}
}
2.5、return 指令
return 指令用于返回状态码给客户端
server,location,if
应用示例:
例9:如果访问的.sh结尾的文件则返回403操作拒绝错误
server {listen 80;server_name www.testpm.cn;#access_log /var/log/nginx/http_access.log main;
location / {root /usr/share/nginx/html;index index.html index.htm;}
location ~* \.sh$ {return 403;}
}
例10:80 ======> 443 :80转443端口
server {listen 80;server_name www.testpm.cn;access_log /var/log/nginx/http_access.log main;return 301 https://www.testpm.cn$request_uri;
}
server {listen 443 ssl;server_name www.testpm.cn;access_log /var/log/nginx/https_access.log main;
#ssl on;ssl_certificate /etc/nginx/cert/2447549_www.testpm.cn.pem;ssl_certificate_key /etc/nginx/cert/2447549_www.testpm.cn.key;ssl_session_timeout 5m;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;ssl_prefer_server_ciphers on;
location / {root /usr/share/nginx/html;index index.html index.htm;}
}
[root@nginx-server ~]# curl -I http://www.testpm.cn
HTTP/1.1 301 Moved Permanently
Server: nginx/1.16.0
Date: Wed, 03 Jul 2019 13:52:30 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://www.testpm.cn/
3、last,break详解
[root@localhost test]# cat /etc/nginx/conf.d/last_break.conf
server {listen 80;server_name localhost;access_log /var/log/nginx/last.access.log main;
location / {root /usr/share/nginx/html;index index.html index.htm;}location /break/ {root /usr/share/nginx/html;rewrite .* /test/break.html break;}location /last/ {root /usr/share/nginx/html;rewrite .* /test/last.html last;}location /test/ {root /usr/share/nginx/html;rewrite .* /test/test.html break;}
}
[root@localhost conf.d]# cd /usr/share/nginx/html/
[root@localhost html]# mkdir test
[root@localhost html]# echo "last" > test/last.html
[root@localhost html]# echo "break" > test/break.html
[root@localhost html]# echo "test" > test/test.html
http://10.0.105.196/break/break.html
http://10.0.105.196/last/last.html
注意:
- last 标记在本条 rewrite 规则执行完后,会对其所在的 server { … } 标签重新发起请求;
- break 标记则在本条规则匹配完成后,停止匹配,不再做后续的匹配;
- 使用 alias 指令时,必须使用 last;
- 使用 proxy_pass 指令时,则必须使用break。
4、Nginx 的 https ( rewrite )
server {
listen 80;
server_name *.vip9999.top vip9999.top;
if ($host ~* "^www.vip9999.top$|^vip9999.top$" ) {
return 301 https://www.vip9999.top$request_uri;
}
}
# Settings for a TLS enabled server.
server {
listen 443 ssl;
server_name www.vip9999.top;ssl_certificate cert/214025315060640.pem;
ssl_certificate_key cert/214025315060640.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
nginx的set指令相关推荐
- 九爷带你了解 nginx 日志配置指令详解
nginx日志配置指令详解 日志对于统计排错来说非常有利的. 本文总结了nginx日志相关的配置如 access_log.log_format.open_log_file_cache.log_not_ ...
- nginx利用referer指令实现防盗链配置
nginx模块ngx_http_referer_module通常用于阻挡来源非法的域名请求,我们应该牢记.下面这篇文章主要介绍了nginx利用referer指令实现防盗链配置的相关资料,需要的朋友可以 ...
- [Linux] nginx的try_files指令实现隐藏index.php的重写
1.nginx的try_files指令 ,核心功能是替代rewrite,并且比rewrite更强大的是可以按顺序查找文件是否存在,如果文件都找不到才会执行最后的重定向 解决的问题是,如果一个网站的部署 ...
- nginx主模块指令
daemon 语法: daemon on | off 缺省值: on 若为on则以守护进程方式执行,默认为on,否则控制终端结束后,nginx进程也随终端的结束而结束.该选项主要用于开发调试,生产环境 ...
- linux nginx 配置优化,nginx 配置优化指令
worker_processes worker_processes指令是用来设计Nginx进程数,官方默认设为1,赋值太多了,将会对系统IO影响效率,降低Nginx服务器性能.但是为了让多核CPU能够 ...
- Nginx中server_name指令介绍
Nginx中server_name指令介绍 用途 根据官方文档说明,用来设置虚拟服务器,对于用IP还是请求头部中的Host字段内容设置这个指令的值,没有明确的分别. 用法 指令后跟特定域名,此时第一个 ...
- nginx rewrite if指令剖析
0. 前言 nginx的if功能确实是弱得可以,严重影响了生产效率.故此,先提出严正抗议! 1. if指令配置的实现 对于这个功能奇弱的if指令,nginx实现得还特别复杂.下面将对其实现进行剖析. ...
- Nginx 的 default_server 指令
server {listen 80 default_server;server_name oschina.net www.oschina.net;... } 先看看上面这段配置,listen 指令后面 ...
- Nginx的location指令
location指令 server{listen 80;server_name localhost;location / {}location /abc{}... } location:用来设置请求的 ...
- nginx日志配置指令详解
日志对于统计排错来说非常有利的.本文总结了nginx日志相关的配置如access_log.log_format.open_log_file_cache.log_not_found.log_subreq ...
最新文章
- CodeForces #369 div2 D Directed Roads DFS
- Linux网络模拟,模拟网络访问解析
- 凯兑换系统服务器角色,王者荣耀新英雄凯怎么兑换
- rxjs里b = a.pipe(map(mapFn))的执行示意图
- [NOIP2015] 子串
- 位运算应用口诀和实例(转自大笨狼)
- 基于websocket的聊天实现逻辑(springboot)
- u8薪资管理_用友U8习题集
- 2015计算机二级java真题_2015年计算机二级考试java试题
- Lesson 013 —— python 数字
- 全球铁矿石行业供给分析与投资可行性分析报告2022年版
- TASKCTL高可用架构调度服务与安装
- 利用python拼接图片代码_Python实现图片拼接的代码
- csgo服务器搭建(linux)
- 执行 this.$destroy()后原生DOM事件也无法执行
- 25.mc_api介绍及使用示例
- 《经济学人》深度报道:DeepMind和谷歌的AI拉锯战
- csr867x入门之触摸按键配置(十一)
- Clojure CLR 入门
- ABAP 资产类BAPI过账 BAPI_ACC_DOCUMENT_POST
热门文章
- mysql里字典是什么意思_mysql常用字典表(完整版)
- 手机wifi已连接但无法访问互联网_手机已连接但无法访问互联网,碰到这个情况怎么破?想不到是这样...
- ng-zorro自定义主题
- 【DTB/DTBO 分区介绍】
- 用pandas实现剧组表格道具用到faker库
- 分享在github超酷超炫特效动画,不看你会后悔的。
- 水泵状态监测与故障诊断
- UR--MoveIt Setup Assistant and Simulation
- GitHub仓库push报错remote: Support for password authentication was removed on August 13, 2021.
- 计算机硬件保留内存,为硬件保留的内存:8G