博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx rewrite URL examples with and without redirect address
阅读量:5340 次
发布时间:2019-06-15

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

原文地址: http://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-address#.VY9nfJeqqko

Nginx can handle the rewrite parameter differently, depending on the destination syntax. 

Here are some examples how to define redirects and URL rewrites in nginx. 

server { 

    server_name www.example.com; 
    root /var/www/www.example.com; 
    location / { 
        rewrite ^/$ http://websrv1.example.com/mypage redirect; 
    } 

This will result in forwarding the browser to http://websrv1.example.com/mypage. The redirect address will be shown in the address bar.

Let's try this without a redirect or permanent option but with break or last: 

server { 

    server_name www.example.com; 
    root /var/www/www.example.com; 
    location / { 
        rewrite ^/$ http://websrv1.example.com/mypage last; 
    } 
}

Although the rewrite option is now set to last, the browser will still follow the URL and changes the URL in the address bar. 

The reason for this is the http:// which is interpreted as external redirect.

So if you want to keep your domain and simply want to rewrite the URL (like in Apache with mod_rewrite), you must use a relative path:

server { 

    server_name www.example.com; 
    root /var/www/www.example.com; 
    location / { 
        rewrite ^/$ /mypage last; 
    } 
}

This will load the website for www.example.com from the subfolder /mypage within the document root (/var/www/www.example.com). 

But what if the destination website is loaded from somewhere else, for example from a Tomcat server in the background? 

The following configuration covers this:

upstream tomcat { 

    server 127.0.0.1:8080; 
server { 
    server_name www.example.com; 
    root /var/www/www.example.com; 
    location / { 
        include proxy-settings.conf; 
        proxy_pass http://tomcat; 
        rewrite ^/$ /mypage last; 
    } 
}

First everything (location /) is passed to tomcat (the defined upstream server). Then the redirect for the root path (/) is happening and is relative to the path. 

This results in keeping the browser's address URL at www.example.com but loads the website from 127.0.0.1:8080/mypage. 

 

转载于:https://www.cnblogs.com/AloneSword/p/4605269.html

你可能感兴趣的文章
使用iperf测试网络性能
查看>>
struts2入门之准备工作
查看>>
从C语言的弱类型属性说起
查看>>
大牛博客
查看>>
图片的显示隐藏(两张图片,默认的时候显示第一张,点击的时候显示另一张)...
查看>>
Docker 安装MySQL5.7(三)
查看>>
python 模块 来了 (调包侠 修炼手册一)
查看>>
关于CSS的使用方式
查看>>
本地MongoDB服务开启与连接本地以及远程服务器MongoDB服务
查看>>
跨域解决方案之CORS
查看>>
学习RESTFul架构
查看>>
分析语句执行步骤并对排出耗时比较多的语句
查看>>
原生JS轮播-各种效果的极简实现
查看>>
软件工程总结作业---提问回顾与个人总结
查看>>
计数器方法使用?
查看>>
带你全面了解高级 Java 面试中需要掌握的 JVM 知识点
查看>>
sonar结合jenkins
查看>>
解决VS+QT无法生成moc文件的问题
查看>>
AngularJs练习Demo14自定义服务
查看>>
stat filename
查看>>