大网数据广告活动
服务器运维问答

nginx location alias causes error

· 52CE 我爱测

I'm getting an error for nginx with I try to sudo service nginx reload with the following included: location /supermarkets.* { alias /supermarkets } What I'm trying to do is if the link is e.g. 111.111.11.11/supermarkets/somethin…

问题原文

原帖链接 · A. L · CC BY-SA 3.0

I'm getting an error for nginx with I try to sudo service nginx reload with the following included:

location /supermarkets.* {
        alias /supermarkets
}

What I'm trying to do is if the link is e.g. 111.111.11.11/supermarkets/something it should redirect to 111.111.11.11/supermarkets. As context, this is for react-router, I am trying to use browser rather than hash.

Full section

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

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

Use nginx -t to test your configuration and identify syntax errors. Your location statement is incorrect syntax and your alias statement is missing a terminating ;. See this document for details.

You may be confusing the function of the alias directive, see this document for details.

What you are trying to do is usually achieved using a try_files directive.

For example:

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

The above will check URIs beginning with /supermarkets. If the URI matches a local file, it will be returned, otherwise /supermarkets/index.html will be returned.

See this document for more.

I notice that your configuration also has a location ~ \.php$ block which means that any URI beginning with /supermarkets, but ending with .php will not be directed to /supermarkets/index.html.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-09-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。