【问题标题】:nginx redirection to index pagenginx 重定向到索引页面
【发布时间】:2020-11-05 21:01:36
【问题描述】:

我正在努力实现从非索引页面到我的索引页面的自动 nginx 重定向,但 /admin 除外

例如,example.com/test 应该重定向到 example.com,但 example.com/admin 不应该重定向到 example.com

这是我当前的 nginx 配置文件:

upstream app_server {
    server unix:/tmp/mysite.sock;
}

proxy_cache_path /var/www/example.com/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; 

server {
    listen 80;
    server_name www.example.com example.com;

    # redirects both www and non-www to https
    return 301 https://www.example.com$request_uri;
    
}

server {
    listen 443;
    server_name example.com;
    return 301 https://www.example.com$request_uri;
    }

server {

    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;

    server_name  www.example.com;
    
    ssl_certificate     /etc/letsencrypt/live/www.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;

    charset     utf-8;

    client_max_body_size 75M;

    location /media  {
        alias /var/www/example.com/media;
    }

    location /static {
        alias /var/www/example.com/static;
    }

    location / {
        proxy_cache my_cache;
        include proxy_params;
        proxy_pass http://app_server;
        proxy_ssl_server_name on;
    }
}

我尝试在我的 location / 块中添加 try_files 声明和其他内容,但似乎没有任何效果。我错过了什么吗?

【问题讨论】:

    标签: nginx proxy reverse-proxy gunicorn


    【解决方案1】:

    您正在尝试将proxy_passtry_files 混合使用,它不会在同一个location 块中工作。您可以改用命名位置,并使用否定正则表达式断言将任何不以 /admin 开头的 URI 重写为根:

    location / {
        try_files $uri @app;
    }
    location @app {
        rewrite ^(?!/admin) / break;
        proxy_cache my_cache;
        include proxy_params;
        proxy_pass http://app_server;
    }
    

    您不需要单独的 location /media { ... }location /static { ... } 块,因为作为 nginx 文档 states

    当位置与指令值的最后一部分匹配时:

    location /images/ {
        alias /data/w3/images/;
    }
    

    最好改用root 指令:

    location /images/ {
        root /data/w3;
    }
    

    相反,您只需要定义公共服务器根目录(在任何 location 块之外):

    root /var/www/example.com;
    

    您也不需要使用 proxy_ssl_server_name 指令,因为您没有使用 HTTPS 协议将请求代理到上游。

    【讨论】:

    • 谢谢伊万,它就像一个魅力。也感谢您对 root 而不是别名的建议。 (正如预期的那样),它不适用于管理员背后不存在的资源(例如:example.com/admin/wrongurl)。关于如何使这项工作的任何想法?
    • @NicolasBerthier 您希望如何处理这种类型的 URI?作为/admin 而不是/admin/wrongurl?您可以添加作为第一个重写规则rewrite ^/admin.* /admin break;
    • 我认为我没有正确解释我的问题,我的错 :)。有没有办法将所有(且仅)非工作资源重定向到/?当我运行 Django 应用程序时,/admin 后面有一些工作资源,例如example.com/admin/auth/user/1/change。例如,我想 example.com/admin/auth/user/1/change 工作,但不想 example.com/admin/wrongurl
    • @NicolasBerthier 我可以看到两种不同的方法。第一个是通过某种正则表达式过滤所有的 URI(可能在 map 块的帮助下)。如果您知道要传递到后端的所有 URI,则可以使用此方法。第二种是将所有请求传递到后端,然后根据从上游返回的HTTP结果代码进行操作。
    • 谢谢你,伊万。我已经解决了我的问题,方法是使用您的解决方案并将我的 Django 应用程序的管理 URI 添加到“秘密”URL 后面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 2018-04-27
    • 2017-07-18
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多