【问题标题】:Nginx returns 404 for some sub domains but not for othersNginx 对某些子域返回 404,但对其他子域不返回
【发布时间】:2017-02-10 14:39:41
【问题描述】:

所以我正在尝试设置 5 个网站,它们都在同一个域上,只是具有不同的子域,等等 www。和cdn。 但是万维网。工作正常 寿CDN。没有,它得到了我刚刚复制过来的相同文件,文件夹的所有权限都是相同的。 我在自己的文件中都有每个子域等 wwwmydomaincom 和 cdnmydomaincom 并且配置是相同的,唯一的区别是 server_name。有效的文件得到 www.mydomain.com 其余的得到 somesubdomain.mydomain.com 并且他们抛出 404。

我在 ubuntu 服务器 16.04.1 上使用 Nginx。

添加

location / {
  try_files $uri.html;
}

并且子域可以很好地显示 html 页面(现在它们的配置与有效的配置一样) 但是.. 每个资产、css、js、图像或其他东西都会得到 404,所以它是一个纯 html 页面。

下面的配置与 www.mydomain.com 完全相同,但更改为适合 cdn.mydomain.com

server {
  listen 80;
  server_name cdn.domain.com;
  location /.well-known/acme-challenge {
     default_type "text/plain";
     root /storage/webserver/certbot;
  }

  #Forces all other requests to HTTPS
  location / {
     return 301 https://$host$request_uri;
  }
}
server {
  listen 443 ssl http2;
  server_name cdn.domain.com;
  ssl_certificate /etc/letsencrypt/live/cdn.domain.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/cdn.domain.com/privkey.pem;
  ssl_dhparam /etc/ssl/certs/dhparam.pem;
  ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
  ssl_prefer_server_ciphers on;
  ssl_ciphers EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA+SHA512:EECDH+ECDSA+SHA384:EECDH+ECDSA+SHA256:ECDH+AESGCM:ECDH+AES256:DH+AESGCM:DH+AES256:RSA+AESGCM:!aNULL:!eNULL:!LOW:!RC4:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS;
  ssl_session_cache shared:TLS:2m;
  # OCSP stapling
  ssl_stapling on;
  ssl_stapling_verify on;
  resolver 8.8.8.8;
  # Set HSTS to 365 days
  add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains';
  root /storage/webserver/cdn.domain.com;
  index index.html index.php;
  location @rewrite {
    rewrite ^ $uri.php last;
    try_files $uri =404;
  }
  location ~ \.php$ {
    fastcgi_split_path_info ^(.+?\.php)(/.+)$;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include fastcgi.conf;
    try_files $uri =404;
  }
  rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
  #rewrite ^/(.*)/$ /$1 permanent;
  error_page 404 /404.php;
  error_page 500 503 502 504 /error/40x.php;
  location =/error/40x.html {
    internal;
  }
}

【问题讨论】:

    标签: nginx http-status-code-404


    【解决方案1】:

    咳咳,这就是我想学这种东西的原因。

    您显然需要让它查找文件。

    所以如果有人进入这种小情况,不要忘记让它在根文件夹中查找。

    location / {
      try_files $uri $uri/ $uri.html @rewrite;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-20
      • 2018-04-19
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 2014-01-13
      • 2019-07-13
      • 1970-01-01
      相关资源
      最近更新 更多