使用nginx托管多个网站

时间:2017-07-20 13:08:30

标签: javascript ubuntu nginx nginx-location

我有安装nginx的Ubuntu服务器16.04服务器(nginx / 1.10.3)

我只有一个DNS名称energy.mysite.local我需要将其用于多个网站。

我有2个目录,其中我已经放置了网站内容

  1) /var/www/website/
  2) /var/www/web-site/

我的网站 - 可用&已启用网站的文件夹内容2个文件 website & web-site

website的内容

server {

    listen 443 ssl;
    include snippets/self-signed.conf;
    include snippets/ssl-params.conf;

    location /website/ {
             alias /var/www/website/;
             gzip_static on;
             expires max;
             add_header Cache-Control public;
             autoindex off;
    }

    server_name energy.mysite.local/website;

    location / {
     try_files $uri $uri/ =404;
    }
}

web-site的内容

server {

    listen 443 ssl;
    include snippets/self-signed.conf;
    include snippets/ssl-params.conf;

    location /web-site/ {
             alias /var/www/web-site/;
             gzip_static on;
             expires max;
             add_header Cache-Control public;
             autoindex off;
    }

    server_name energy.mysite.local/web-site;

    location / {
     try_files $uri $uri/ =404;
    }
}

这样我才能使用以下网站

https://energy.mysite.local/website/1.0.1/src/abc.js

https://energy.mysite.local/web-site/2.0.1/src/abc.js

目前https://energy.mysite.local/web-site/2.0.1/src/abc.js工作正常。

但是https://energy.mysite.local/website/1.0.1/src/abc.js正在给我404页面

任何方法来完成它?

1 个答案:

答案 0 :(得分:1)

根据您的问题,这两个位置似乎共享相同的根,在这种情况下,可能不需要单独的location blocksalias directive

例如:

server {
    listen 443 ssl;
    server_name energy.mysite.local;

    include snippets/self-signed.conf;
    include snippets/ssl-params.conf;

    root /var/www;

    gzip_static on;
    expires max;
    add_header Cache-Control public;

    location / {
        try_files $uri $uri/ =404;
    }
}