子域路由的行为与Nginx中的域不同

时间:2018-06-15 00:02:27

标签: nginx nginx-location

我遇到了一个问题,指向域example.com会返回404,而指向同一资源的子域stage.example.com会按预期运行。我在Ubuntu上用Nginx 1.10.3运行它。

以下是此服务器的配置文件(/etc/nginx/sites-available/example.com符号链接到/etc/nginx/sites-enabled/example.com)。

server {
    # Root directory
    root /home/username/example.com;

    # Index
    index index.php index.html index.htm index.nginx-debian.html;

    # Server name(s)
    server_name stage.example.com example.com www.example.com;

    # Create access and error logs in /var/log/nginx
    # access_log /var/log/nginx/example_com-access_log;
    # error_log /var/log/nginx/example_com-error_log info;


    # Generic location
    location / {
            # First attempt to serve request as file, the fall back
            # to allow hashless routing
            try_files $uri $uri/ /index.php;
    }

    location ~ \.json$ {
            # Patching existing loading of JSON via post
            # To allow POST on static pages
            error_page  405     =200 $uri;
    }

    # Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;

             # With php7.0-fpm:
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;

            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    # route to headless use of wordpress
    location /content/ {
            try_files $uri $uri/ /content/index.php$is_args$args;
    }

    # route to api in headless wordpress
    location ~ ^/api/ {
        # if permalinks not enabled
        rewrite ^/api/(.*?)$ /?rest_route=/$1 last;
    }

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

思想?

1 个答案:

答案 0 :(得分:1)

这在我的配置中无法正确处理IPv6时出现了问题。像这样更新我的服务器配置解决了它:

server {
    # Port(s)
    listen                          80 default_server;
    listen                          [::]:80 ipv6only=on;

    # Remainder of config identical to sample provided
}

这就是我找到解决方案的原因:https://www.digitalocean.com/community/questions/ipv6-connectivity-issues-with-nginx