Linux下nginx服务实现http到http的自动重定向

续我的上篇博文:https://mp.csdn.net/postedit/89554311即本篇博文是在上篇博文修改完之后的nginx.conf文件中进行修改的。

 

实现http到http的自动重定向

 

1、实现当访问http://xin.westos.org/bbs/时,自动跳转到http://bbs.westos.org/

 

(1)先配置一个虚拟主机,并进行测试,以保证配置的虚拟主机是正确的。

[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf   #在http模块中编写如下的server模块(编写151-159行内容,160行的}是http模块自带的})
151     server {
152         listen 80;
153         server_name bbs.westos.org;
154     
155         location / {
156                 root /bbs;
157                 index index.html;
158         }
159     }
160 }


[root@server1 ~]# mkdir /bbs
[root@server1 ~]# echo bbs.westos.org > /bbs/index.html

[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload   #编写完配置文件之后,重载nginx服务


[root@foundation83 ~]# vim /etc/hosts   #在物理机上编写本地解析文件,以确保bbs.westos.org有解析
172.25.83.1     bbs.westos.org

 

(2)实现当访问http://xin.westos.org/bbs/时,自动跳转到http://bbs.westos.org/

[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf   #将之前编写的140行注释,添加141行的网页重写的命令
137     server {
138         listen 80;
139         server_name xin.westos.org;
140         #rewrite ^/(.*)$ https://xin.westos.org/$1 permanent;
141         rewrite ^/bbs$ http://bbs.westos.org/ permanent;   #表示访问http://xin.westos.org/bbs时自动跳转到http://bbs.westos.org
142         #set_real_ip_from 172.25.83.0/24;
143         #real_ip_header X-Forwarded-For;
144         #real_ip_recursive on;
145 
146         #location / { 
147                 #return 200 "client real ip: $remote_addr\n";
148                 #root /web;
149                 #index index.html;
150         #}   
151     }
152     server {
153         listen 80;
154         server_name bbs.westos.org;
155 
156         location / {
157                 root /bbs;
158                 index index.html;
159         }
160     }
161 }

[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload   #修改完配置文件之后,重载nginx服务

 

(3)进行测试:

 

测试一:在命令行进行测试

[root@server1 ~]# curl -I xin.westos.org/bbs
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 09:13:27 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://bbs.westos.org/   #我们会发现,实现了xin.westos.org/bbs——>http://bbs.westos.org的自动跳转
   

 

测试二:在浏览器中访问:http://xin.westos.org/bbs,帮我们自动跳转到了下面的界面。

2、实现当访问http://xin.westos.org/bbs/...时,自动跳转到http://bbs.westos.org/...(即对1的内容进行改进)

 

在进行改进之前,我们进行一个测试,来看出改进的必要性

[root@server1 ~]# curl -I xin.westos.org/bbs/index.html
HTTP/1.1 404 Not Found
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 09:17:28 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive   #在这里,我们会发现一个问题(在bbs后面添加了/index.html之后,并没有实现重定向),这显然是不合理的

在浏览器中访问:http://xin.westos.org/bbs/index.html,却没有帮我们自动跳转到http://bbs.westos.org。

下面,我们进行改进

 

(1)编写nginx.conf文件

[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf   #将之前编写的141行注释,添加142行的网页重写的命令
137     server {
138         listen 80;
139         server_name xin.westos.org;
140         #rewrite ^/(.*)$ https://xin.westos.org/$1 permanent;
141         #rewrite ^/bbs$ http://bbs.westos.org/ permanent;
142         rewrite ^/bbs/(.*)$ http://bbs.westos.org/$1 permanent;
143         #set_real_ip_from 172.25.83.0/24;
144         #real_ip_header X-Forwarded-For;
145         #real_ip_recursive on;
146 
147         #location / { 
148                 #return 200 "client real ip: $remote_addr\n";
149                 #root /web;
150                 #index index.html;
151         #}   
152     }
153     server {
154         listen 80;
155         server_name bbs.westos.org;
156 
157         location / {
158                 root /bbs;
159                 index index.html;
160         }
161     }
162 }


[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload   #编写完配置文件之后,重载nginx服务

 

(2)进行测试:

 

测试一:在命令行进行测试

[root@server1 ~]# curl -I xin.westos.org/bbs/index.html
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 09:21:57 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://bbs.westos.org/index.html  #我们会发现,实现了xin.westos.org/bbs/index.html——>http://bbs.westos.org/index.html的自动跳转
   

 

测试二:在浏览器中访问:http://xin.westos.org/bbs/index.html,帮我们自动跳转到了下面的界面。

 

3、实现当访问http://bbs.westos.org时,自动跳转到http://xin.westos.org/bbs/...

 

(1)编写nginx.conf文件

[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf   #将之前编写的142行注释,添加143行,144行和145行的网页重写的命令,在139行的server_name变量中添加bbs.westos.org,去掉150,152,153,154行的注释,并将之前编写的bbs.westos.org的虚拟主机注释。
137     server {
138         listen 80;
139         server_name xin.westos.org bbs.westos.org;
140         #rewrite ^/(.*)$ https://xin.westos.org/$1 permanent;
141         #rewrite ^/bbs$ http://bbs.westos.org/ permanent;
142         #rewrite ^/bbs/(.*)$ http://bbs.westos.org/$1 permanent;
143         if ($host = "bbs.westos.org"){
144                 rewrite ^/(.*)$ http://xin.westos.org/bbs/$1 permanent;
145         }
146         #set_real_ip_from 172.25.83.0/24;
147         #real_ip_header X-Forwarded-For;
148         #real_ip_recursive on;
149 
150         location / { 
151                 #return 200 "client real ip: $remote_addr\n";
152                 root /web;
153                 index index.html;
154         }   
155     }
156     #server {
157         #listen 80;
158         #server_name bbs.westos.org;
159 
160         #location / {
161                 #root /bbs;
162                 #index index.html;
163         #}
164     #}
165 }


[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload   #编写完配置文件之后,重载nginx服务

 

(2)进行测试:

 

[root@server1 ~]# vim /etc/hosts   #编写本地解析文件
172.25.83.1    bbs.westos.org

[root@server1 ~]# cp -r /bbs/ /web/   #将bbs目录拷贝到/web目录下
[root@server1 ~]# ll /web/
total 8
drwxr-xr-x 2 root root 24 Apr 26 17:43 bbs
-rw-r--r-- 1 root root 15 Apr 26 15:13 index.html
-rw-r--r-- 1 root root  5 Apr 26 16:24 test.html

 

测试一:在命令行进行测试

[root@server1 ~]# curl -I bbs.westos.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 09:46:09 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://xin.westos.org/bbs/   #我们可以看到实现了自动跳转(bbs.westos.org——>https://xin.westos.org/bbs)

[root@server1 ~]# curl -I bbs.westos.org/index.html
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 09:46:12 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://xin.westos.org/bbs/index.html  #我们可以看到实现了自动跳转(bbs.westos.org/index.html——>https://xin.westos.org/bbs/index.html)

 

测试二:在浏览器中访问:http://bbs.westos.org/,帮我们自动跳转到了下面的界面。

在浏览器中访问:http://bbs.westos.org/index.html,帮我们自动跳转到了下面的界面。

4、实现当访问http://test.westos.org时报错(test.westos.org该域名没有被定义为虚拟主机)

 

在配置“访问http://test.westos.org时报错”之前,我们先来访问下test.westos.org,以形成对比。

[root@foundation83 ~]# vim /etc/hosts   #编辑本地解析文件
172.25.83.1     test.westos.org

从上图中,我们可以看到,我们访问到的是nginx服务的默认发布页,这是为什么呢?肯定是配置文件决定的呀。下面,我们来查看配置文件

[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf
  1 load_module modules/ngx_http_image_filter_module.so;
  2 user  nginx nginx;
  3 worker_processes  auto;
  4 
  5 #error_log  logs/error.log;
  6 #error_log  logs/error.log  notice;
  7 #error_log  logs/error.log  info;
  8 
  9 #pid        logs/nginx.pid;
 10 
 11 
 12 events {
 13     worker_connections  65536;
 14 }
 15 
 16 
 17 http {
 18     include       mime.types;
 19     default_type  application/octet-stream;
 20 
 21     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 22     #                  '$status $body_bytes_sent "$http_referer" '
 23     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 24 
 25     #access_log  logs/access.log  main;
 26 
 27     sendfile        on;
 28     tcp_nopush     on;
 29     tcp_nodelay     on;
 30 
 31     #keepalive_timeout  0;
 32     keepalive_timeout  65;
 33 
 34     #gzip  on;
 35 
 36     limit_conn_zone $binary_remote_addr zone=addr:10m;
 37     limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
 38 
 39     server {
 40         listen       80;
 41         server_name  localhost;
 42         set_real_ip_from 172.25.83.0/24;
 43         real_ip_header X-Forwarded-For;
 44         real_ip_recursive on;
 45 
 46         #charset koi8-r;
 47 
 48         #access_log  logs/host.access.log  main;
 49 
 50         location / {
 51             root   html;
 52             index  index.html index.htm;
 53         }
 54 
 55         location /download/ {
 56                 limit_conn addr 1;
 57                 #limit_rate 50k;
 58                 limit_req zone=one burst=5;
 59                 image_filter resize 100 200;
 60                 autoindex on;
 61         }
 62 
 63         #error_page  404              /404.html;
 64 
 65         # redirect server error pages to the static page /50x.html
 66         #
 67         error_page   500 502 503 504  /50x.html;
 68         location = /50x.html {
 69             root   html;
 70         }
 71 
 72         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 73         #
 74         #location ~ \.php$ {
 75         #    proxy_pass   http://127.0.0.1;
 76         #}
 77 
 78         location ~ .*\.(jpg|png|css|js)?$ {
 79                 expires 30d;
 80         }
 81 
 82         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 83         #
 84         #location ~ \.php$ {
 85         #    root           html;
 86         #    fastcgi_pass   127.0.0.1:9000;
 87         #    fastcgi_index  index.php;
 88         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 89         #    include        fastcgi_params;
 90         #}
 91 
 92         # deny access to .htaccess files, if Apache's document root
 93         # concurs with nginx's one
 94         #
 95         #location ~ /\.ht {
 96         #    deny  all;
 97         #}
 98     }
 99 
100 
101     # another virtual host using mix of IP-, name-, and port-based configuration
102     #
103 
104     #server {
105     #    listen       8000;
106     #    listen       somename:8080;
107     #    server_name  somename  alias  another.alias;
108 
109     #    location / {
110     #        root   html;
111     #        index  index.html index.htm;
112     #    }
113     #}
114 
115 
116     # HTTPS server
117     #
118     server {
119         listen       443 ssl;
120         server_name  xin.westos.org;
121 
122         ssl_certificate      cert.pem;
123         ssl_certificate_key  cert.pem;
124 
125         ssl_session_cache    shared:SSL:1m;
126         ssl_session_timeout  5m;
127 
128         ssl_ciphers  HIGH:!aNULL:!MD5;
129         ssl_prefer_server_ciphers  on;
130 
131         location / {
132             root   /web;
133             index  index.html index.htm;
134         }
135     }
136 
137     server {
138         listen 80;
139         server_name xin.westos.org bbs.westos.org;
140         #rewrite ^/(.*)$ https://xin.westos.org/$1 permanent;
141         #rewrite ^/bbs$ http://bbs.westos.org/ permanent;
142         #rewrite ^/bbs/(.*)$ http://bbs.westos.org/$1 permanent;
143         if ($host = "bbs.westos.org"){
144                 rewrite ^/(.*)$ http://xin.westos.org/bbs/$1 permanent;
145         }
146         #set_real_ip_from 172.25.83.0/24;
147         #real_ip_header X-Forwarded-For;
148         #real_ip_recursive on;
149 
150         location / {
151                 #return 200 "client real ip: $remote_addr\n";
152                 root /web;
153                 index index.html;
154         }
155     }
156     #server {
157         #listen 80;
158         #server_name bbs.westos.org;
159 
160         #location / {
161                 #root /bbs;
162                 #index index.html;
163         #}
164     #}
165 }

通过观察nginx.conf文件,我们发现在nginx.conf文件中,我们定义了三个server{}模块,

  1. 第一个模块是:访问http://localhost/...(或http://172.25.83.1/...)时,去访问/usr/local/nginx/html文件
  2. 第二个模块是:https模块
  3. 第三个模块是:访问http://bbs.westos.org/...时重定向到http://xin.westos.org/bbs/...;访问http://xin.westos.org时,去访问/web目录

我们会发现,根本就没有定义test.westos.org模块,所以访问http://test.westos.org/...时就定位到默认模块(第一个模块了)。因此访问的就是nginx服务的默认发布页。

 

 

(1)编写nginx.conf文件

[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf   #将41行的localhost修改为_,并在下一行添加42行对应的内容
 39     server {
 40         listen       80;
 41         server_name  _;
 42         return 500;
 43         set_real_ip_from 172.25.83.0/24;
 44         real_ip_header X-Forwarded-For;
 45         real_ip_recursive on;
 46 
 47         #charset koi8-r;
 48 
 49         #access_log  logs/host.access.log  main;
 50 
 51         location / {
 52             root   html;
 53             index  index.html index.htm;
 54         }


[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload   #编写完配置文件之后,重载nginx服务

 

(2)进行测试:

在浏览器中访问:test.westos.org

 

5、实现当访问http://test.westos.org时,自动跳转到http://xin.westos.org

 

(1)编写nginx.conf文件

[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf   #将42行注释,并在下一行添加43行对应的内容
 39     server {
 40         listen       80;
 41         server_name  _;
 42         #return 500;
 43         rewrite ^(.*) http://xin.westos.org;
 44         set_real_ip_from 172.25.83.0/24;
 45         real_ip_header X-Forwarded-For;
 46         real_ip_recursive on;
 47 
 48         #charset koi8-r;
 49 
 50         #access_log  logs/host.access.log  main;
 51 
 52         location / {
 53             root   html;
 54             index  index.html index.htm;
 55         }


[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload   #编写完配置文件之后,重载nginx服务

 

(2)进行测试:

在浏览器中访问:test.westos.org

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值