为什么我的Nginx别名重定向不起作用(转到404)?

时间:2017-10-04 04:26:26

标签: redirect nginx alias

我的主网站位于x.com(@ /var/www/x.com/index.html

# MAIN LOCATION
location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
    #autoindex on;
    proxy_set_header X-Real-IP $remote_addr;
}

我希望 / v2 重定向到另一个本地目录(@ /var/www/x.com/v2/public/index.html

# Redirect beta site
location /v2 {
        alias    /var/www/x.com/v2/public;
}

这似乎应该可行,但相反,它会进入我的404网站。不确定这是否重要,但这是我的两个上面的根源:

# path
root    /var/www/throneoflies.com/html;

我尝试在" /" 及以下上方订购" / v2" - 似乎没有做出改变。我读过我不应该使用' root'而不是别名'因为它是一个不同的架构(/ v2 / public /而不仅仅是/ v2 /)。

编辑:似乎它应该正常工作 - 自从这篇文章以来我已经阅读了很多内容。这是我的完整档案:

server {
        # MAIN >>
        # SSL configuration
        listen                  443 default_server ssl;
        server_name             www.x.com;
        error_page 404          /error/404.html;
        server_tokens           off;

        # SSL
        ssl_certificate         /etc/ssl/x.com/cert.pem;
        ssl_certificate_key     /etc/ssl/x.com/key.pem;
        ssl_protocols           TLSv1 TLSv1.1 TLSv1.2;
        #ssl_session_tickets    off;
        ssl_ciphers             EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
        ssl_prefer_server_ciphers on;

        # path
        root                    /var/www/x.com/html;
        #root                   /var/www/x.com/v2/public; #works!

        # Add index.php to the list if you are using PHP
        #index index.php index.html index.htm index.nginx-debian.html;
        index                   index.html;

        # 404 handling - no cache
        location = /404.html {
                add_header      Cache-Control "no-cache" always;
        }

        # Redirect beta site : Why doesn't it work if the outer block root changed to the same path works? I can REPLACE my "/" with this v2 path np. However, alias at /v2 does not work.
        location = /v2 {
                alias           /var/www/x.com/v2/public;
                #root           /var/www/x.com/v2/public;
                #try_files      $uri $uri/ =404;
        }

        # MAIN LOCATION
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files       $uri $uri/ =404;
                #autoindex      on;
                proxy_set_header X-Real-IP $remote_addr;

        # DENY .HTACCESS ACCESS
        location ~/\.ht {
                deny all;
        }
}

1 个答案:

答案 0 :(得分:1)

这显然应该由=中的location = /v2引起,$uri只会与/v2正好=的请求相匹配;最正确的方法是删除location /v2 {alias /var/www/x.com/v2/public;}

此外,请注意,如果使用/var/www/x.com/v2/public.bak,则人们也可以访问/v2.bakvar objDate = new Date("11/19/2017"), locale = "en-us", month = objDate.toLocaleString(locale, { month: "short" }); alert(month + ' ' + objDate.getDate() + ' ' + objDate.getFullYear()); 之类的内容;如果那不是故意的话,那么你最好还是酌情使用斜杠。