配置nginx服务两个网站 [英] Configure nginx to serve two websites
问题描述
目前我正在使用 nginx 来提供我的 API 文档 (mywebsites.com) 的静态文件,我想添加另一个网站:mywebsites.com/subwebsite.
所以我编辑了 nginx 配置文件来为它们提供服务:
currently I'm using the nginx to serve the static files of my API docs (mywebsites.com), I want to add another website : mywebsites.com/subwebsite.
so I edited the nginx config file to serve both of them :
server {
listen 0.0.0.0:80;
server_name mywebsite.com;
client_max_body_size 1m;
access_log /var/log/nginx/error.log;
error_log /var/log/nginx/static.log;
#location ~ /\.git {
# deny all;
#}
location /subwebsite {
root /home/api/portal/build;
index index.html index.htm;
try_files $uri $uri/ =404;
}
location / {
root /home/api/application/public;
index index.html index.htm;
try_files $uri $uri/ =404;
}
#sendfile off;
}
问题是当我尝试访问新网站时:mywebsite.com/subwebsite .. 找不到 404.
当我尝试更改当前服务器以转发到新的子网站(而不是添加位置/子网站,我更改位置/的根目录)时,它可以工作.
原始文件:
The problem is when I try to access the new website : mywebsite.com/subwebsite .. I got 404 not found.
And when I try to change the current server to forward to the new subwebsite (instead of adding location /subwebsite, I change the root for location /) it works.
the original file:
server {
listen 0.0.0.0:80;
server_name mywebsite.com;
client_max_body_size 1m;
access_log /var/log/nginx/error.log;
error_log /var/log/nginx/static.log;
location ~ /\.git {
deny all;
}
location ~ {
root /home/api/application/public;
index index.html index.htm;
try_files $uri $uri/ =404;
}
sendfile off;
}
我在这里缺少什么?提前致谢
What I'm missing here ?
Thanks in advance
推荐答案
我认为,这种位置变体可能适合您:
I think, this variant of location, could work for you:
location /subwebsite {
root /home/api/portal/build;
try_files $uri /index.html;
}
这样的配置文件似乎更具可读性:
It seems that such config file could be more readable:
server {
listen 0.0.0.0:80;
server_name mywebsite.com;
root /home/api/application/public;
index index.html index.htm;
client_max_body_size 1m;
access_log /var/log/nginx/error.log;
error_log /var/log/nginx/static.log;
#location ~ /\.git {
# deny all;
#}
location /subwebsite {
root /home/api/portal/build;
try_files $uri /index.html;
}
#sendfile off;
}
这篇关于配置nginx服务两个网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!