环境准备:
mkdir -p /var/log/nginx/
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.xcf.com;
#修改域名
#charset koi8-r;
access_log /var/log/nginx/www.xcf.com-access.log;
##日志保存路径修改
location / {
if ($host = 'www.xcf.com') {
#$host为rewrite全局变量,代表请求主机头字段或主机名
rewrite ^/(.*)$ http://www.zxc.com/$1 permanent;
#$1为正则匹配得内容,即域名后边得字符串
}
root html;
index index.html index.htm;
}
echo "192.168.126.14 www.xjj.com" >> /etc/hosts
echo "192.168.126.13 www.xcf.com" >> /etc/hosts
systemctl restart nginx.service
此时打开浏览器,访问 http://www.xjj.com 时会自动跳转至http://www.xcf.com
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.xjj.com;
#charset koi8-r;
access_log /var/log/nginx/www.xcf.com-access.log;
##设置是否是合法的IP标记
set $rewrite true;
#设置变量$rewrite,变量值为boole值true
#判断是否为合法IP
if ($remote_addr = "192.168.126.14") {
set $rewrite false;
#当客户端IP为192.168.126.15时,将变量值设为flase,不进行重写
}
##除了合法IP,其它都是非法IP,进行重写跳转到维护页面
if ($rewrite = true) {
#当变量值为true时,进行重写
rewrite (.+) /weihu.html;
#重写在访问IP后边插入/weihu.html,例如192.168.126.65/weihu.html
}
location = /weihu.html {
root /var/www/html;
#页面返回/var/www/html/weihu.html的内容
}
location / {
root html;
index index.html index.htm;
mkdir -p /var/www/html
echo '<h1>weihu!!</h1>' > /var/www/html/weihu.html
systemctl restart nginx.service
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name bbs.xjj.com;
#修改域名
#charset koi8-r;
access_log /var/log/nginx/www.xjj.com-access.log;
#添加
location /post {
rewrite (.+) http://www.xjj.com/bbs$1 permanent;
#这里$1为位置变量,代表/post
}
location / {
root html;
index index.html index.htm;
}
mkdir -p /usr/local/nginx/html/bbs/post
echo "this is 1.html" >> /usr/local/nginx/html/bbs/post/1.html
echo "192.168.126.14 bbs.xjj.com" >> /etc/hosts
systemctl restart nginx.service
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.xjj.com;
#修改域名
#charset koi8-r;
access_log /var/log/nginx/www.xjj.com-access.log;
if ($request_uri ~ ^/100-(100|200)-(\d+)\.html$) {
#设置正则匹配
rewrite (.*) http://www.xjj.com permanent;
#设置重写
}
location / {
root html;
index index.html index.htm;
}
systemctl restart nginx.service
server {
listen 80;
server_name www.xjj.com;
#修改域名
#charset koi8-r;
access_log /var/log/nginx/www.xjj.com-access.log;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.xjj.com permanent;
}
location / {
root html;
index index.html index.htm;
}
systemctl restart nginx.service
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.xjj.com;
#修改域名
#charset koi8-r;
access_log /var/log/nginx/www.xjj.com-access.log;
location ~* /abc/123.html {
rewrite (.+) http://www.xjj.com permanent;
}
location / {
root html;
index index.html index.htm;
}
systemctl restart nginx.service
①rewrite
②location:主要功能proxy_pass
③if :location下,只支持单分支不支持多分支
匹配文件: location中 ~ 和 ~/*,区分大小写 精确,不区分更为精确
匹配目录: location 中 ~/* 和 ~,区分大小写 更为精确,优先级更高
①location / {} 加快加载速度
②location /static {} 静态请求匹配
③location 反向代理 反向跳转到配置文件中upstream tomcat_server地址池中,获取发送到后端节点的“目标_IP",跳转的方式直接使用proxy_pass http://tomact_server (函数名)
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_53560205/article/details/120752186
内容来源于网络,如有侵权,请联系作者删除!