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

uno*_*017 6 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;
        }

    }
Run Code Online (Sandbox Code Playgroud)

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

如果对 /admin 应用程序有任何点击,应用程序应该返回 /Login 页面,我正在查看 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" "-"
Run Code Online (Sandbox Code Playgroud)

它显示 nginx 代理 /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;
Run Code Online (Sandbox Code Playgroud)

它可以工作,但 CSS 不行,因为所有 css 文件 URL 都不在 nginx 配置上!!!!

And*_*kov 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;
}
Run Code Online (Sandbox Code Playgroud)

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