zoukankan      html  css  js  c++  java
  • Nginx rewrite URL examples with and without redirect address

    原文地址: 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. 

  • 相关阅读:
    hdu2060
    hdu1003
    style属性
    变量与常量
    使用BIgDecimal进行浮点数的精确计算
    CSUST 玩游戏 题解(思维+优先队列维护第k大)
    百度之星 迷失 题解(矩阵快速幂+分层图)
    CSUST 简单数学题 题解(质因子分解+并查集)
    CSUST 神秘群岛 题解(LCA)
    CSUST lh的简单图论 题解(图转树LCA问题)
  • 原文地址:https://www.cnblogs.com/AloneSword/p/4605269.html
Copyright © 2011-2022 走看看