NGINX在一个域上的多个应用程序

时间:2016-09-02 17:55:21

标签: nginx

我在相同服务器上有2个应用程序,具有相同域的不同端口[8080,8090]。

我已将nginx配置如下:

server {
        listen 80 ;
        server_name  XXX.XXX.XXX;
        root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location /{

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://localhost:8080;
        }

        location /admin {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://localhost:8090;
        }

    }

问题是端口8080上的第一个应用程序工作正常,但其他未加载的应用程序CSS收到404错误。似乎它指向/目录。处理这种情况的最佳方法是什么?

如果有任何命中/ admin应用程序,应用程序应该返回/登录页面我正在寻找nginx日志:

xx.xx.xx.xx - - [03/Sep/2016:09:00:35 +0000] "GET /admin/ HTTP/1.1" 302 0 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0" "-"
xx.xx.xx.xx - - [03/Sep/2016:09:00:36 +0000] "GET /Login HTTP/1.1" 404 0 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0" "-"

它显示nginx proxy / admin到正确的应用程序然后/ Login不在配置上,所以它返回404,如果我将配置更改为以下内容:

location /admin {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://localhost:8090/Login;

它的工作,但CSS没有,再次coz所有的css文件URL不在nginx配置!!!!

1 个答案:

答案 0 :(得分:0)

您应该以另一种方式提供静态文件,并绕过向后端发送静态文件的请求。

location /{...}

之前添加下一部分
# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/  {
  root    /usr/share/nginx/html/<folder with your static files>;
  include /etc/nginx/mime.types;
  expires 30d;
}

请在此处查看其他设置的信息示例https://www.nginx.com/resources/wiki/start/topics/examples/full/#

相关问题