【问题标题】:How to correct my nginx configuration if it is wrong?如果错误,如何更正我的 nginx 配置?
【发布时间】:2019-10-29 17:40:28
【问题描述】:

我是 nginx 新手。我尝试使用搜索 www 和 stackoverflow 来学习。大多数情况下,我会得到帮助来了解如何构建我的 nginx 配置。

我的域是 www.mysite.com。一切; 着陆页错误服务器错误必须重定向到默认index.html。我还定义了我的访问和错误日​​志。所有这些都在 /etc/nginx/conf.d/default.conf 中完成(如下代码)。

我需要重定向 (proxy_pass) /admin/user 以及与它们相关的任何内容。例如 /admin 也有不同的文件夹,如 /admin/login/。我需要重定向 /admin 之后的所有内容。 /user 也是如此。

1) 查看我的代码,我是否正确重定向了 location /adminlocation /user

2) 我也用 try_files $uri $uri/ =404;在重定向中。它还将 404 重定向到默认 index.html。我做得对吗?

3) 我也拒绝访问某些文件和文件夹。我做得对吗?

我的主要问题是如何纠正我的 nginx 配置,如果它是错误的?因此,为了理解正确的 nginx 配置,我将我的问题分为上述 3 个不同的问题。我希望我没有阻止 stackoverflow 如何提出问题指南。

谢谢。

更新:

server {
    charset UTF-8;
    listen 80;
    listen [::]:80;

    server_name www.mysite.com;
    access_log  /var/log/nginx/host.access.log  main;
    error_log  /var/log/nginx/host.error.log  main;

    # define a root location variable and assign a value
    set $rootLocation /usr/share/nginx/html;

    # define www.mysite.com landing page to the static index.html page
    location / {
        root   rootLocation;
        index  index.html index.htm;
    }

    # define error page to the static index.html page
    error_page 404  /index.html;
    location = /index.html {
        root rootLocation;
        internal;
    }

    # redirect server error pages to the static index.html page
    error_page   500 502 503 504  /index.html;
    location = /index.html {
        root   rootLocation;
        internal;
    }

    # redirect www.mysite.com/admin to localhost
    # /admin or /admin/ or /admin/**** must be redirect to localhost
    location /admin {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass "http://127.0.0.1:3000";
    }

    # redirect www.mysite.com/user to localhost
    # /user or /user/ or /user/**** must be redirect to localhost   
    location /user {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass "http://127.0.0.1:3001";
    }
}

【问题讨论】:

    标签: nginx redirect configuration proxypass


    【解决方案1】:

    通常在server 块中放置一次root 语句,而不是在多个location 块中重复相同的值。


    您正在使用 proxy_pass 在将 URI 传递到上游之前更改它。在这种情况下,location 值和proxy_pass 值的 URI 部分应该都以 / 结尾,或者都不以 / 结尾。详情请见this document

    您通常希望将try_filesproxy_pass 放在同一个location 中。这会导致 Nginx 在允许请求通过上游之前检查其文档根目录中的文件是否存在。


    您不需要拒绝对配置文件的访问,因为这些文件首先不应位于文档根目录中。

    【讨论】:

    • 谢谢理查德。首先如何在一个地方定义root并使用其他地方?其次,我更新了proxy_pass。 Nginx 和 localhost/3000 或 3001 它在同一台机器上。我在哪里定义 try_files?
    • 在我的示例中,/admin 表示与 /admin 相关的任何内容,例如 /admin 或 /admin=***** 或 /admin/login。我的配置 /admin 是正确的还是应该将其更改为 /admin/*
    • root 的值是从外部块继承的,除非您在 location 块中重新定义它。您不需要location /admin/location /user/ 块中的try_files 语句。声明location /admin/ 表示任何以/admin/ 开头的URI(有关详细信息,请参阅this document)。
    • 感谢 Richard 我更新了我的代码。只有无法获得它的部分是proxy_pass。 (对不起我的英语不好)。您提供的链接谈论“....原始请求URI ...”。在我的情况下,如果 uri maces ( /admin ) 我需要过去那个 uri 而不是像我的示例中显示的那样更改 URI。如果我的 uri 是(例如 /admin=?123 或 /admin/login/1 ),nginx 是否正确使用 proxy_pass 重定向我的 uri?
    • 您问题中的proxy_pass 语句包含一个URI 部分,即/。这会导致 Nginx 修改请求的 URI 并从原始请求中删除 adminuser。如果您想在不修改的情况下向上游传递整个 URI,请从 proxy_pass 语句中删除尾随的 /
    猜你喜欢
    • 2021-06-10
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多