重构与重写

重构与重写,第1张

Cento7+Nginx之URL重写

cent7nginx的URL重写

大家在上一篇文章中写了一个Cento7Nginx反向代理完成了301次以上的跳转。今天重点介绍Cento7Nginx的URL重写。

主机名:A-S.IXMSOFT.COM

IP:192.168.5.20

角色:Nginx服务器

大家要提前做好安装Nginx仓库的准备。

Yum install http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm yum install nginx

安装完成后,可以查询nginx环境变量的默认设置。

vim /etc/nginx/nginx.conf

默认nginx.conf文档

user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf;

正如您所看到的,nginx的默认设置包含一个包含几个环境变量的选项,因此您通常可以保持该默认设置的环境变量不变。

每个人都查询默认的环境变量。

每个人都查询默认设置的default.conf文档内容。

您可以看到这个环境变量已经占用了端口80,因此您将更改端口80。

接下来,我们将创建一个新的环境变量,它也可以在默认的环境变量中进行更改。但是不推荐。

cd /etc/nginx/conf.d/ vim rewrite.conf server { listen 80; server_name ixmsoft.com; location / { rewrite ^/(.*) http://www.baidu.com; } access_log logs/rewrite_access.log; }

存储撤回后,大家重启nginx。

systemctl stop nginx systemctl start nginx

必须把server_name的域名跳转到nginxIP才能浏览。

在dns中添加记录,d1.ixmsoft.com倾向于NginxIP192.168.5.20。

然后检测浏览。

然后每个人都查询日志,

cat /etc/nginx/logs/rewrite_access.log

自然,您可以添加几个服务器来完成301跳转,并在原始环境变量内升级它们。

server { listen 80; server_name d1.ixmsoft.com; location / { rewrite ^/(.*) http://www.baidu.com; } access_log logs/rewrite_access.log; } server { listen 80; server_name d2.ixmsoft.com; location / { rewrite ^/(.*) http://www.sina.com; } access_log logs/rewrite_access02.log; }

重启后,大家检查浏览。

相同的查询日志

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/778210.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-03
下一篇 2022-05-03

发表评论

登录后才能评论

评论列表(0条)

保存