Redirect Nginx-错误的重定向?

Redirect Nginx-错误的重定向?,redirect,nginx,http-status-code-404,config,lets-encrypt,Redirect,Nginx,Http Status Code 404,Config,Lets Encrypt,我使用Nginx作为web服务器与LetsEncrypt一起加密我的连接。 我的主页有多个子域名,如www.domain.com、mail.domain.com等。 现在我想将所有“/.well-known”连接重定向到一个特殊的根文件夹 我想我有正确的配置,但结果不是我想要的 以下是我的配置: 默认值。vhost #============================================= #== Server #================================

我使用Nginx作为web服务器与LetsEncrypt一起加密我的连接。 我的主页有多个子域名,如www.domain.com、mail.domain.com等。 现在我想将所有“/.well-known”连接重定向到一个特殊的根文件夹

我想我有正确的配置,但结果不是我想要的

以下是我的配置:

默认值。vhost

#=============================================
#== Server
#==============================================

server {

        #=============================================
        #== General
        #=============================================

        # Port
        listen 80;

        # Server name
        #server_name _;

        #=============================================
        #== Locations
        #=============================================

        location /.well-known {
                #default_type "text/plain";
                root /var/wwww/LetsEncrypt;
        }

        location / {
                return 301 https://www.example.com$request_uri;
        }
}


server {

        #=============================================
        #== General
        #=============================================

        # Port
        listen 443 ssl;

        # Server name
        #server_name _;

        #=============================================
        #== Locations
        #=============================================

        location / {
                return 301 https://www.example.com$request_uri;
        }
}
www.domain.com

#=============================================
#== HTTP-Server
#=============================================

server {

        #=============================================
        #== General
        #=============================================

        # Port
        listen 443 ssl default_server;

        # Server name
        server_name www.example.com;

        # Root folder
        root /var/www/www.example.com/web/;

        # Order of index files
        index index.php index.html index.htm;

        #=============================================
        #== Includes
        #=============================================

        # SSL configuration
        include /etc/nginx/ssl.conf;

        # PHP configuration
        include /etc/nginx/php.conf;

        #=============================================
        #== Locations
        #=============================================

        location /.well-known/acme-challenge {
                #default_type "text/plain";
                root /var/wwww/LetsEncrypt;
        }

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ /\.ht {
                deny all;
        }
}
#=============================================
#== HTTP-Server
#=============================================

server {

        #=============================================
        #== General
        #=============================================

        # Port
        listen 443 ssl;

        # Server name
        server_name mail.example.com;

        # Root folder
        root /var/www/mail.example.com/web/;

        # Order of index files
        index index.php index.html index.htm;

        #=============================================
        #== Includes
        #=============================================

        # SSL configuration
        include /etc/nginx/ssl.conf;

        # PHP configuration
        include /etc/nginx/php.conf;

        #=============================================
        #== Locations
        #=============================================

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ /\.ht {
                deny all;
        }
}
mail.domain.com

#=============================================
#== HTTP-Server
#=============================================

server {

        #=============================================
        #== General
        #=============================================

        # Port
        listen 443 ssl default_server;

        # Server name
        server_name www.example.com;

        # Root folder
        root /var/www/www.example.com/web/;

        # Order of index files
        index index.php index.html index.htm;

        #=============================================
        #== Includes
        #=============================================

        # SSL configuration
        include /etc/nginx/ssl.conf;

        # PHP configuration
        include /etc/nginx/php.conf;

        #=============================================
        #== Locations
        #=============================================

        location /.well-known/acme-challenge {
                #default_type "text/plain";
                root /var/wwww/LetsEncrypt;
        }

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ /\.ht {
                deny all;
        }
}
#=============================================
#== HTTP-Server
#=============================================

server {

        #=============================================
        #== General
        #=============================================

        # Port
        listen 443 ssl;

        # Server name
        server_name mail.example.com;

        # Root folder
        root /var/www/mail.example.com/web/;

        # Order of index files
        index index.php index.html index.htm;

        #=============================================
        #== Includes
        #=============================================

        # SSL configuration
        include /etc/nginx/ssl.conf;

        # PHP configuration
        include /etc/nginx/php.conf;

        #=============================================
        #== Locations
        #=============================================

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ /\.ht {
                deny all;
        }
}
那么,发生了什么。。mail.domain.com和www.domain.com非常有效。 问题在于这条众所周知的规则。Nginx忽略了它,我总是被重定向,而不是从“/var/www/LetsEncrypt”获取文件

我认为默认的_服务器是问题所在,但是在将其移动到“default.vhost”之后,什么都不起作用(总是404..听起来也有点奇怪…)

我希望你们能帮助我:(

亲切的问候,
Andreas

首先,您需要使用,它将捕获从指定目录开始的所有请求

location ^~ /.well-known/acme-challenge {
其次是
/var/wwww/LetsEncrypt
(4个w),而不是
/var/www/LetsEncrypt

您的位置块应如下所示:

location ^~ /.well-known/acme-challenge {
    #default_type "text/plain";
    root /var/www/LetsEncrypt;
}
还要注意和root之间的区别,因为root会将uri附加到路径,例如:

alias /var/www/LetsEncrypt; # Will serve files from /var/www/LetsEncrypt

root /var/www/LetsEncrypt; # Will serve files from /var/www/LetsEncrypt/.well-known/acme-challenge

如果您试图提供特定目录,这可能会使您绊倒。

首先,您需要使用,它将捕获从指定目录开始的所有请求

location ^~ /.well-known/acme-challenge {
其次是
/var/wwww/LetsEncrypt
(4个w),而不是
/var/www/LetsEncrypt

您的位置块应如下所示:

location ^~ /.well-known/acme-challenge {
    #default_type "text/plain";
    root /var/www/LetsEncrypt;
}
还要注意和root之间的区别,因为root会将uri附加到路径,例如:

alias /var/www/LetsEncrypt; # Will serve files from /var/www/LetsEncrypt

root /var/www/LetsEncrypt; # Will serve files from /var/www/LetsEncrypt/.well-known/acme-challenge

如果您试图提供特定目录,这可能会使您绊倒。

好的,我稍后会尝试。感谢您的快速回复:)因此,我已经测试了更改,效果非常好!只有“别名”而不是“根”不起作用,但问题不大。我会把你的答案设为接受答案!好的,我稍后再试试。感谢您的快速回复:)所以,我已经测试了这些更改,它工作得非常完美!只有“别名”而不是“根”不起作用,但问题不大。我会把你的答案设为接受答案!