在 Nginx Ubuntu 14.04 上托管多个应用程序

在 Nginx Ubuntu 14.04 上托管多个应用程序

我很难找到解决方案。我有一台运行 NGINX 的 Ubuntu 14.04 机器。我想托管以下 2 个文件夹。

/var/www/apphost.comp.ill.com/app1/home/index.html /var/www/apphost.comp.ill.com/app2/index.html

我想在访问“apphost.comp.ill.com/app1”时打开app1的索引文件,并在访问“apphost.comp.ill.com/app2”时打开app2的索引文件。

我相信我需要编辑“/etc/nginx/sites-available/apphost.comp.ill.com”才能实现这一点,但我似乎不知道如何实现。我尝试了多种方法,在网上搜索但找不到任何解决方案。这是我的文件当前的样子:

server {
    listen 80;
    listen [::]:80;

    root /var/www/apphost.comp.ill.com/app1/home;     
    index index.html index.htm home home.html;

    # Make site accessible from http://localhost/
    server_name apphost.comp.ill.com;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

当我访问 apphost.comp.ill.com 时,它适用于 app1。我怎样才能使它在我转到“apphost.comp.ill.com/app1”时工作,并且在我转到“apphost.comp.ill.com/app2”时添加app2以工作。

请帮忙。谢谢

答案1

我尝试了这个并且它有效:

server {
    listen 80;
    listen [::]:80;

    index index.html index.htm home home.html;

    # Make site accessible from http://localhost/
    server_name apphost.comp.ill.com;

    location /app1 {
            # we need to use alias here to strip the "app1" from 
            # the requested path
            alias /var/www/apphost.comp.ill.com/app1/home;

            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }    
    location /app2 {
            # we don't need an alias here because app2 matches the
            # directory structure
            root /var/www/apphost.comp.ill.com/;

            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }
}

我建议 /app1 和 /app2 不带前导斜杠,因为在我的测试中,浏览到 .../app1 不起作用。

别名命令的文档:http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

答案2

编辑:这个答案是错误的,因为“location”之后的部分将被追加到文档根目录中。 (即nginx尝试打开文件/var/www/apphost.comp.ill.com/app1/home/app1/index.html)我不应该在没有先尝试的情况下发布它,抱歉。

你有没有尝试过:

server {
    listen 80;
    listen [::]:80;

    index index.html index.htm home home.html;

    # Make site accessible from http://localhost/
    server_name apphost.comp.ill.com;

    location /app1/ {
            root /var/www/apphost.comp.ill.com/app1/home;
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }    
    location /app2/ {
            root /var/www/apphost.comp.ill.com/app2;
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }
}

相关内容