首页 > 解决方案 > 配置nginx服务两个网站

问题描述

目前我正在使用 nginx 来提供我的 API 文档(mywebsites.com)的静态文件,我想添加另一个网站:mywebsites.com/subwebsite。
所以我编辑了 nginx 配置文件来为它们提供服务:

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。
当我尝试更改当前服务器以转发到新的子网站时(而不是添加位置 / 子网站,我更改了位置 / 的根目录)它可以工作。
原始文件:

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;
}


我在这里缺少什么?提前致谢

标签: reactjsnginxstatic-files

解决方案


我认为,这种位置变体可能对您有用:

location /subwebsite {
    root  /home/api/portal/build;
    try_files $uri /index.html;
}

这样的配置文件似乎更具可读性:

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;
}

推荐阅读