nginx 位置别名导致错误

nginx 位置别名导致错误

sudo service nginx reload当我尝试执行下列操作时,出现 nginx 错误:

location /supermarkets.* {
        alias /supermarkets
}

我想做的是,如果链接是,111.111.11.11/supermarkets/something它应该重定向到111.111.11.11/supermarkets。作为上下文,这是针对 react-router 的,我试图使用浏览器而不是哈希。

完整部分

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    #listen 443 ssl default_server;
    #listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

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

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

    #   # With php7.0-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

        location /supermarkets.* {
                alias /supermarkets
        }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}

答案1

用于nginx -t测试您的配置并识别语法错误。您的location语句语法不正确,并且alias缺少终止符;。请参阅这个文件了解详情。

您可能会混淆该指令的功能alias,请参阅这个文件了解详情。

您尝试做的事情通常是使用try_files指令来实现的。

例如:

location /supermarkets {
    try_files $uri /supermarkets/index.html;
} 

以上代码将检查以 开头的 URI /supermarkets。如果 URI 与本地文件匹配,则返回该文件,否则/supermarkets/index.html返回 。

这个文件了解更多信息。

我注意到您的配置中也有一个location ~ \.php$块,这意味着任何以 开头/supermarkets但以 结尾的URI.php都不会被定向到/supermarkets/index.html

相关内容