使用Nginx在子目录中提供静态文件

时间:2018-06-06 07:43:56

标签: nginx static subdirectory serve

我目前在域名khairulslt.me(来自NameCheap)上有一堆工作静态文件。最近,我尝试设置子域名(khairulslt.me/RGBGame),如下面的代码所示;但是,我一直收到404错误。我错过了什么?

server {
  listen 80;

  index circles.html;
  server_name khairulslt.me www.khairulslt.me;

  location / {
  root /var/www/khairulslt.me;
  add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy- 
  revalidate, max-age=0';
  expires off;
  }

  location /RGBGame {
  alias /var/www/RGBGame/colorGame.html;
  index colorGame.html;
  }
}

PS:我想将新文件作为工作网络应用程序提供给我在圈子应用程序中使用的相同数字海洋液滴。

3 个答案:

答案 0 :(得分:0)

您不需要提供文件的路径,而是提供别名指令中的目录。

所以简单地使用:

location /RGBGame/ { 
    alias /var/www/RGBGame/; 
    index colorGame.html; 
}

答案 1 :(得分:0)

您的别名似乎指向文件colorGame.html试一试:

server {
  listen 80;

  server_name khairulslt.me www.khairulslt.me;


  location /RGBGame/ {
     alias /var/www/RGBGame/;
  }
}

使用alias时,对://khairulslt.me/RGBGame/file.foo的请求将提供以下文件:

/var/www/RGBGame/file.foo

例如,您可以使用root(将位置附加到路径):

  location /RGBGame/ {
     root /var/www/khairulslt.me/;
  }

在这种情况下,对://khairulslt.me/RGBGame/file.foo的请求将提供以下文件:

/var/www/khairulslt.me/RGBGame/file.foo

答案 2 :(得分:0)

解决了它。 这里需要更改这段代码:

location /RGBGame {
root /var/www/khairulslt.me;
index colorGame.html;
try_files $uri $uri/ /var/www/RGBGame/colorGame.html?q=$uri&$args;
autoindex off;
}

将保留我的最终配置以防万一:

server {

  server_name khairulslt.me www.khairulslt.me;

  autoindex off;
  location / {
    root /var/www/khairulslt.me;
    index circles.html;
  }


listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/khairulslt.me/fullchain.pem; # managed by 
Certbot
ssl_certificate_key /etc/letsencrypt/live/khairulslt.me/privkey.pem; # managed 
by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

location /RGBGame {
    root /var/www/khairulslt.me/RGBGame;
    index colorGame.html;
    try_files $uri $uri/ /var/www/RGBGame/colorGame.html?q=$uri&$args;
    autoindex off;
  }

location /robots.txt { return 200 "User-agent: *\nDisallow: /\n"; 
  }

}

 server {
if ($host = www.khairulslt.me) {
    return 301 https://$host$request_uri;
  } # managed by Certbot


if ($host = khairulslt.me) {
    return 301 https://$host$request_uri;
  } # managed by Certbot


listen 80;
server_name khairulslt.me www.khairulslt.me;
return 404; # managed by Certbot
}

此配置的作用:

1)在URL上发送静态文件,Web App#1,又名一堆html / css / js文件)khairulslt.me

2)在网址khairulslt.me/RGBGame上提供第二组静态文件,Web App#2,又名一堆html / css / js文件