我们经常上网,仔细的人会发现一个现象。当在浏览器的地址栏中输入 baidu.com 或 www.baidu.com 最后的连接都是www.baidu.com,这是 因为 baidu.com 跳转到 www.baidu.com 连接。那么我们来看看Nginx中是如何实现这一过程的呢。
- 修改 ww.conf 文件,如下:
另一种配置方式:[root@NFS nginx]# vim conf/extra/www.conf server { listen 80; server_name daydayup.com; rewrite ^/(.*) http://www.daydayup.com/$1 permanent; } server { listen 80; server_name www.daydayup.com; root html/www; index index.html index.htm; }
server { listen 80; server_name www.daydayup.com daydayup.com; if ($host ~* "^daydayup.com$") { rewrite ^/(.*) http://www.daydayup.com/$1 permanent; } root html/bbs; index index.html index.htm; }
- 重启Nginx服务。
[root@NFS nginx]# sbin/nginx -s reload
- 测试
无论在浏览器中输入 daydayup.com 还是 www.daydayup.com ,最终地址栏中显示的是 www.daydayup.com 。
- 用curl命令测试。
1.先在/etc/hosts 文件中作域名 daydayup.com 的解析。[root@NFS nginx]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.16.1.31 NFS www.daydayup.com bbs.daydayup.com blog.daydayup.com daydayup.com
2.curl daydayup.com 测试。[root@NFS nginx]# curl daydayup.com <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.12.2</center> </body> </html> [root@NFS nginx]#
# curl daydayup.com :访问daydayup.com ,但是daydayup.com跳转到其他地址就不再继续访问,此时如果想要继续深入追踪,则采用 curl -L daydayup.com。
[root@NFS nginx]# curl -L daydayup.com #跟踪转发地址,深入访问到最后一层 10.0.0.31 www.daydayup.com [root@NFS nginx]# [root@NFS nginx]# curl -Lv daydayup.com #显示中途转发的详细信息 * About to connect() to daydayup.com port 80 (#0) * Trying 172.16.1.31... connected * Connected to daydayup.com (172.16.1.31) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2 > Host: daydayup.com > Accept: */* > < HTTP/1.1 301 Moved Permanently < Server: nginx/1.12.2 < Date: Sun, 01 Dec 2019 12:48:19 GMT < Content-Type: text/html < Content-Length: 185 < Connection: keep-alive < Location: http://www.daydayup.com/ < * Ignoring the response-body * Connection #0 to host daydayup.com left intact * Issue another request to this URL: 'http://www.daydayup.com/' * About to connect() to www.daydayup.com port 80 (#1) * Trying 172.16.1.31... connected * Connected to www.daydayup.com (172.16.1.31) port 80 (#1) > GET / HTTP/1.1 > User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2 > Host: www.daydayup.com > Accept: */* > < HTTP/1.1 200 OK < Server: nginx/1.12.2 < Date: Sun, 01 Dec 2019 12:48:19 GMT < Content-Type: text/html < Content-Length: 27 < Last-Modified: Sat, 30 Nov 2019 07:24:15 GMT < Connection: keep-alive < ETag: "5de2191f-1b" < Accept-Ranges: bytes < 10.0.0.31 www.daydayup.com * Connection #1 to host www.daydayup.com left intact * Closing connection #0 * Closing connection #1 [root@NFS nginx]#
最后修改于 2019-12-04 15:06:24
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付

