如果nginx配置错误,如何更正?

如果nginx配置错误,如何更正?,nginx,redirect,configuration,proxypass,Nginx,Redirect,Configuration,Proxypass,我是nginx的新手。我试图学习使用搜索www和stackoverflow。大多数情况下,我会得到帮助,了解如何构建我的nginx配置 我的域名是www.mysite.com。一切登录页,错误和服务器错误必须重定向到default index.html。我还定义了我的访问和错误日志。所有这些都是在/etc/nginx/conf.d/default.conf中完成的(代码下面) 我需要重定向(proxy\u pass)/admin、/user以及与它们相关的任何内容。示例/admin还有不同的文件

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

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

我需要重定向(proxy\u pass/admin/user以及与它们相关的任何内容。示例/admin还有不同的文件夹,如/admin/login/。我需要将/admin之后的所有内容重新定向。对于/用户也是如此

1)查看我的代码我是否正确重定向了位置/管理位置/用户

2)我还使用try_文件$uri$uri/=404;在重定向中。它还将404重定向到default index.html。我做得对吗

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

我的主要问题是,如果nginx配置错误,如何更正它?因此,为了理解正确的nginx配置,我将问题分为上面3个不同的问题。我希望我没有忘记如何提问

多谢各位

更新:

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";
    }
}

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


您正在使用
proxy\u pass
在向上游传递URI之前更改URI。在这种情况下,
位置
值和
代理传递
值的URI部分应该都以
/
结尾,或者都不以
/
结尾。有关详细信息,请参阅

通常,您不想将
try_文件
proxy_pass
放在相同的
位置
。这会导致Nginx在允许请求向上游传递之前,检查文档根目录中是否存在该文件



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

谢谢。首先,如何在1个位置定义根并使用其他位置?第二,我要看代理通行证。Nginx和localhost/3000或3001在同一台机器上。我在哪里定义try_文件?在我的示例中,/admin表示与/admin相关的任何内容,例如/admin或/admin=****或/admin/login。因此,我的配置/admin是否正确,或者我是否应该将其更改为/admin/*根
的值继承自外部块,除非您在
位置
块中重新定义它。您不需要
location/admin/
location/user/
块中的
try\u files
语句。语句
location/admin/
表示以
/admin/
开头的任何URI(有关详细信息,请参阅)。感谢Richard我更新了我的代码。唯一不能得到它的部分是代理通行证。(对不起我的英语)。您提供的链接涉及“…原始请求URI…”。在我的例子中,如果uri是maches(/admin),我需要通过该uri,而不是像我的示例中所示的那样更改uri。如果我的uri是(ex/admin=?123或/admin/login/1),nginx是否正确使用proxy\u pass重定向我的uri?问题中的
proxy\u pass
语句包括一个uri部分,即
/
。这会导致Nginx修改请求的URI并从原始请求中删除
admin
user
。如果要在不修改的情况下向上游传递整个URI,请从
proxy\u pass
语句中删除尾部的
/