【问题标题】:Nginx rule for specific URL's特定 URL 的 Nginx 规则
【发布时间】:2015-09-02 19:24:47
【问题描述】:

我的网站上有这个 url 组

mywebsite.com.br/

mywebsite.com.br/sao-paulo/sp

mywebsite.com.br/rio-de-janeiro/rj

mywebsite.com.br/natal/rn

mywebsite.com.br/aparecida-do-rio-doce/go

...

总共有 5561 个不同的网址。有一种方法可以为所有这些 url 发送相同的 html 文件吗?但是,还有一些其他 URL 必须转发到我的 nodejs 服务器,例如:

    mywebsite.com.br/update-password/1234

    mywebsite.com.br/update-password/

    mywebsite.com.br/user/confirm

    mywebsite.com.br/user/confirm/123

    mywebsite.com.br/api/v1/auth/facebook

    mywebsite.com.br/api/v1/auth/local

    mywebsite.com.br/api/v1/user/new

    mywebsite.com.br/api/v1/user/statistics

如何设置 Nginx 模式为第一组 url(5561 个不同的 url)提供相同的 html 文件,并将第二组转发到我的 nodejs 服务器?

【问题讨论】:

    标签: url nginx url-rewriting proxy rule


    【解决方案1】:

    这是使用地图的一种方式:

    map $uri $forward2nodejs {
        ~^/update-password/ 1;
        ~^/user/confirm 1;
        ~^/api/v1/auth/ 1;
        ~^/api/v1/user/ 1;
    }
    
    server {
        server_name mywebsite.com.br;
    
        # default location for the 5561 different urls
        location / {
            try_files /default.html =404;
        }
    
        if ($forward2nodejs) {
            return 301 http://nodejs;       
        }
    }
    

    这是另一个在前缀位置使用 proxy_pass 的方法:

    server {
        server_name mywebsite.com.br;
    
        # default location for the 5561 different urls
        location / {
            try_files /default.html =404;
        }
    
        location /update-password/ {
            include nodejs_proxy_pass;       
        }
    
        location /user/confirm {
            include nodejs_proxy_pass;       
        }
    
        location /api/v1/auth/ {
            include nodejs_proxy_pass;       
        }
    
        location /api/v1/user/ {
            include nodejs_proxy_pass;       
        }
    }
    

    【讨论】:

    • putnamhill 感谢您的回答,这几乎就是我所需要的。我需要对本地运行的 nodejs 服务器执行 proxy_pass,到与 $forward2nodejs 映射模式匹配的 url,而不是重定向 301。这可能吗?
    • 由于 if 语句中不允许使用 proxy_pass,因此您需要尝试不同的方法。我会建议在您的节点 proxy_pass 指令中包含包含文件的前缀位置。我会把它添加到我的答案中,这样你就可以看到它的样子。
    猜你喜欢
    • 2018-07-14
    • 1970-01-01
    • 2012-07-23
    • 2016-12-03
    • 2013-10-12
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多