代码之家  ›  专栏  ›  技术社区  ›  bjnsn

在nginx中,子域路由的行为与域不同

  •  0
  • bjnsn  · 技术社区  · 7 年前

    我遇到了一个指向域的问题 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 回复  |  直到 7 年前
        1
  •  1
  •   bjnsn    7 年前

    在我的配置中,这是一个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