Nginx服务器下的地址重写配置
发布日期:2021-06-29 06:55:36 浏览次数:3 分类:技术文章

本文共 2088 字,大约阅读时间需要 6 分钟。

 可以解决生产环境中以下问题:

1.所有访问a.html的请求,重定位到b.html;

2.所有访问192.168.4.5的请求重定位至某个域名;

3.所有访问192.168.4.5/下面子页面,重定位至某个域名/下相同的页面;

4.实现firefox与curl访问相同页面文件,返回不同的内容。

关于Nginx服务器的地址重写,主要用到的配置参数是rewrite:

-rewrite regex replacement flag

-rewrite 旧地址 新地址 [选项]

问题一的解决思路:

[root@proxy html]# vim /usr/local/nginx/conf/nginx.conf

在对应的server中添加代码

rewrite /a.html /b.html redirect;

如果需要在浏览器显示的是真实的访问路径,则添加代码

rewrite /a.html /b.html redirect;

添加效果如下:

server {

        listen       80;

        server_name  www.a.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
            rewrite /a.html /b.html redirect;
        }

问题二的解决思路:所有访问192.168.4.5的请求重定位至某个域名;

[root@proxy html]# vim /usr/local/nginx/conf/nginx.conf

在对应的server中添加代码

rewrite ^/  http://www.baidu.cn/;

添加效果如下:

server {

        listen       80;
        server_name  www.a.com;
        auth_basic "Input Password:";
        auth_basic_user_file "/usr/local/nginx/pass";
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        rewrite ^/ http://www.baidu.cn/;
        location / {
            root   html;
            index  index.html index.htm;
            #rewrite /a.html /b.html redirect;

        }

问题三的解决思路:所有访问192.168.4.5/下面子页面,重定位至某个域名/下相同的页面;

[root@proxy html]# vim /usr/local/nginx/conf/nginx.conf

在对应的server中添加代码

    rewrite ^/(.*) http://www.tmooc.cn/$1;

添加效果如下:

server {

        listen       80;
        server_name  www.a.com;
        auth_basic "Input Password:";
        auth_basic_user_file "/usr/local/nginx/pass";
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        rewrite ^/(.*) http://www.tmooc.cn/$1;
        location / {
            root   html;
            index  index.html index.htm;

        }

问题四的解决思路:实现firefox与curl访问相同页面文件,返回不同的内容

1.做好测试页面

[root@proxy ~]# echo "this is the firefox page" > /usr/local/nginx/html/firefox/test.html

[root@proxy ~]# echo "this is the curl Page" > /usr/local/nginx/html/test.html

2.在相应的server添加代码

     if ($http_user_agent ~* firefox) {

         rewrite ^(.*)$ /firefox/$1;

          }

添加效果如下:

server {

         listen       80;
       server_name  www.a.com;
        #charset koi8-r;
 
          location / {
              root   html;
              index  index.html index.htm;
            
 
 }
          if ($http_user_agent ~* firefox) {
         rewrite ^(.*)$ /firefox/$1;

          }

}

3.进行测试

[root@proxy ~]# firefox http://192.168.4.5/test.html

[root@proxy ~]# curl http://192.168.4.5/test.html

 

      

转载地址:https://blog.csdn.net/zhydream77/article/details/80206509 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:虚拟化之KVM 『 virsh命令』
下一篇:Tmux-----Linux终端下的复用

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月28日 06时36分14秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章