企业级 ##Nginx网页地址重定向拓展##

本文详细介绍了如何使用Nginx进行虚拟主机配置、网站加密设置及各种重定向操作。包括基本配置步骤、证书生成方法以及不同场景下的重定向实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.建立虚拟主机:

1.编辑主配置文件,添加网页名

[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf

这里写图片描述

[root@server1 conf]# mkdir  /www1
[root@server1 conf]# mkdir  /www2
[root@server1 conf]# cd /www1/
[root@server1 www1]# vim index.html

这里写图片描述

[root@server1 www1]# cd /www2/
[root@server1 www2]# vim index.html

这里写图片描述

[root@server1 www2]# nginx -s reload  ##重启服务

2.【真机】添加解析测试:

[root@foundation39 Desktop]# vim /etc/hosts

这里写图片描述
这里写图片描述
这里写图片描述

二.网站加密:

[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf

这里写图片描述

[root@server1 ~]# cd /etc/pki/tls/certs/
[root@server1 certs]# make cert.pem
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:shaanxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:server1
Email Address []:root@localhost
[root@server1 certs]# cp cert.pem /usr/local/nginx/conf/
[root@server1 certs]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 certs]# nginx -s reload

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

三.重定向:
1.输入www.westos.org 或 westos.org 都可以访问
(1)修改配置文件

[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述
(2)测试

[root@foundation39 Desktop]# curl -I www.westos.org
HTTP/1.1 200 OK                          ##访问成功
Server: nginx/
Date: Tue, 07 Aug 2018 08:55:26 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Tue, 07 Aug 2018 07:55:54 GMT
Connection: keep-alive
ETag: "5b69508a-f"
Accept-Ranges: bytes

[root@foundation39 Desktop]# curl -I westos.org
HTTP/1.1 200 OK                   ##westos.org也能访问成功
Server: nginx/
Date: Tue, 07 Aug 2018 08:55:44 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 07 Aug 2018 01:53:21 GMT
Connection: keep-alive
ETag: "5b68fb91-264"
Accept-Ranges: bytes

2.永久重定向加密(网站前面可加任意字符)
(1)修改配置文件

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述
(2)测试

[root@foundation39 Desktop]# curl -I westos.org
HTTP/1.1 301 Moved Permanently                 ##301永久重定向
Server: nginx/
Date: Tue, 07 Aug 2018 09:03:39 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://www.westos.org                 ##把www.westos.com重定向为                                                                                                     https://www.westos.org

[root@foundation39 Desktop]# curl -I www.westos.org
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:03:57 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://www.westos.org

3.访问www.westos.org/index.html重定向到https://www.westos.org/index.html
(1)修改配置文件

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述
(2)测试

[root@foundation39 Desktop]# curl -I www.westos.org/index.html         ##后面加一个路径
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:09:29 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://www.westos.org/index.html        ##后面路径不会改变,前面重定向

[root@foundation39 Desktop]# curl -I westos.org/index.html
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:09:42 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://www.westos.org/index.html

4.网站后面加任意路径,不会影响重定向结果

[root@server1 ~]# cd /www2/
[root@server1 www2]# vim index.html
[root@server1 www2]# nginx -s reload

这里写图片描述
【真机】添加解析:

[root@foundation39 Desktop]# vim /etc/hosts

这里写图片描述
这里写图片描述

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述

【测试】

[root@foundation39 Desktop]# curl bbs.westos.org
bbs.westos.org                                      ## 可以访问
[root@foundation39 Desktop]# curl  -I  www.westos.org/bbs
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:31:54 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://www.westos.org/bbs

5.访问www.westos.org/bbs跳转到bbs.westos.org

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述

测试:

[root@foundation39 Desktop]# curl  -I  www.westos.org/bbs
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:33:40 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://bbs.westos.org

6.重定向网址前端,后面的不变:

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述

[root@foundation39 Desktop]# curl  -I  www.westos.org/bbs/index.html
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:38:11 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://bbs.westos.orgindex.html

7.网址前面后后面都重定向:

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述
这里写图片描述

测试:

[root@foundation39 Desktop]# curl  -I  bbs.westos.org
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:46:39 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: http://www.westos.org/bbs//
### 解决Nginx配置引起的网页无限重定向问题 当遇到由Nginx配置引发的网页无限重定向问题时,通常是因为重定向逻辑设计不当造成的。具体来说,在某些情况下,`rewrite`指令或`return`语句可能会导致请求被不断重新发送到同一位置,形成循环。 对于这种情况,有几种方法可以用来排查并解决问题: #### 1. 检查是否存在重复的条件判断 如果在同一上下文中多次应用相同的重定向规则,则可能导致不必要的额外跳转。例如,在HTTP向HTTPS转换的过程中,应该只在一个地方执行此操作,而不是在多个虚拟主机定义内分别处理[^4]。 #### 2. 使用适当的状态码 确保选择了合适类型的重定向状态码——临时(`302`)还是永久(`301`)。错误的选择可能会影响浏览器缓存行为以及搜索引擎索引方式。特别是当使用`permanent`(即`301`)时要格外小心,因为它会被客户端长期记住[^1]。 #### 3. 避免无终止条件的递归调用 当使用像`last`这样的标志来指示完成内部重写过程时,务必确认已经提供了清晰明确的退出路径;否则,这将引起最多十次迭代后的服务器错误响应(500 Internal Server Error)[^3]。 #### 4. 审核正则表达式的准确性 仔细审查用于匹配URL模式的任何正则表达式,以防止它们意外捕获不应当参与特定重定向前缀的所有流量。比如上面的例子中使用的通用通配符`.`*.*`可能会过于宽泛[^2]。 为了更好地理解上述建议的应用场景,请参阅下面给出的一个改进版配置片段作为示范: ```nginx server { listen 80; server_name example.com; # 将所有http请求转向https版本 if ($scheme != "https") { return 301 https://$host$request_uri; } location /old-path/ { # 对于旧链接做一次性的永久重定向至新地址 rewrite ^/old-path/(.*)$ /new-location/$1 permanent; } } ``` 通过以上措施,应能有效减少乃至消除因不当配置而产生的无限重定向现象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值