通过Nginx中的子域提供静态内容 [英] Serve static content through subdomain in nginx
问题描述
我有一些slate
文档作为网站,并希望通过以下子域在内部服务器上提供它们:internal-docs.mysite.com
.作为记录,访问mysite.com
显示"nginx正在正确运行"页面.
I have some slate
docs as website and would like to serve them on the internal server, through a subdomain as follows: internal-docs.mysite.com
. For the record, accessing mysite.com
shows the "nginx is running propertly" page.
我用以下路径和名称创建了一个配置文件:/etc/nginx/sites-available/internal-docs.mysite.com
:
I've created a config file with following path and name: /etc/nginx/sites-available/internal-docs.mysite.com
:
server {
listen 80;
server_name internal-docs.mysite.com;
root /var/www/docs-internal;
index index.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
}
当然,我已将文件放在/var/www/docs-internal
中.然后我在/etc/nginx/sites-enabled
目录中建立了一个符号链接到上显示的配置文件:
And of course, I've put the files in /var/www/docs-internal
. And then I made a symlink to the uppershown config file in the /etc/nginx/sites-enabled
dir:
internal-docs.mysite.com -> ../sites-available/internal-docs.mysite.com
然后我重新加载nginx -s reload
,但是访问URL时出现无法访问此站点"错误.
Then I reload nginx -s reload
but "this site can't be reached" error is what I get when accessing the URL.
设置和配置对我来说看起来是正确的(根据我遵循的准则),所以这就是为什么我陷入困境的原因……
The setup and configuration look correct to me (according to the guidelines I've followed), so that's why I'm in a dead end, sort of...
推荐答案
似乎您忘记了Listen指令.请尝试以下操作:
It seems you forgot the Listen directive. Try the following:
server {
listen 80;
server_name internal-docs.mysite.com;
root /var/www/docs-internal;
index index.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
}
如果这不起作用,请检查:
If that does not work, check:
-
该Nginx用户具有对网站内容的读取权限.例如,如果您的Nginx用户是www,并且您具有root用户访问权限,请执行以下操作:
That Nginx user has read permission to the site content. For example if your Nginx user is www and you have root access, do the following:
# su www
$ cat /var/www/docs-internal/index.html
如果失败,请确保该位置具有正确的所有权和权限.请注意,要使用户能够浏览目录,该目录必须具有该用户或用户组的执行位.
If that fails, ensure the location has correct ownership and permissions. Note that for a user to be able to browser a directory, that directory must have the execute bit for that user or user group.
该Nginx用户具有对文件../sites-available/internal-docs.mysite.com的读取权限.例如,如果您的Nginx用户是www,并且您具有root用户访问权限,请执行以下操作:
That Nginx user has read permission on file ../sites-available/internal-docs.mysite.com. For example if your Nginx user is www and you have root access, do the following:
# su www
$ cat /etc/nginx/sites-available/internal-docs.mysite.com
如果失败,请确保配置文件具有正确的所有权.注意:通常Nginx主进程由root运行,并且该进程产生以Nginx用户身份运行的子进程,因此对配置文件的权限不太可能成为问题.
If that fails, ensure that the config files have correct ownership. Note: normally Nginx master process is run by root, and that process spawns sub-processes run as Nginx user, so permissions on config files are unlikely to be the problem.
也许您的配置文件名应该以".conf"结尾(在我的服务器上,我有以下一行: include conf.d/*.conf; ,因此它将不要加载任何以".com"结尾的conf文件.
That maybe your config file name should end with ".conf" (on my server I have the following line: include conf.d/*.conf; so it will NOT load any conf file ending with ".com".
Nginx尝试在其主配置文件中加载../sites-available/中的文件.也许不是,而是在conf.d目录(默认值)中查找.
That Nginx tries to load files in ../sites-available/ in its main config file. Maybe it does not and looks instead in the conf.d directory (the default).
您可以在子域上执行ping和nslookup.如果不能,则必须首先修复该问题(DNS,防火墙...).
That you can do a ping and nslookup on the subdomain. If you cannot, then you have to fix that first (DNS, firewall...).
这篇关于通过Nginx中的子域提供静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!