【问题标题】:Nginx to serve static files and also proxy to backend serverNginx 提供静态文件并代理到后端服务器
【发布时间】:2019-06-11 22:02:23
【问题描述】:

我使用 nginx 来提供静态文件,并代理到后端 java 服务器。我在后端 java 服务器中使用模板语言,最终将替换所有 html 文件。

我不知道 nginx,所以我想就最有效的方法寻求帮助。

文件:

/assets             // Lots more files in this folder
/index.html
/android-chrome-192x192.png
/android-chrome-512x512.png
/apple-touch-icon.png
/browserconfig.xml
/favicon.ico
/favicon-16x16.ico
/favicon-32x32.ico
/mstile-15x150.png
/safari-pinned-tab.svg
/site.webmanifest

到目前为止,这是我的 conf 文件。我提供静态文件,但不代理:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /root/web;
    index index.html;
    server_name _;

    location /assets/ {
        try_files $uri =404;
        sendfile on;
        sendfile_max_chunk 512k;
    }

    location / {
        try_files $uri =404;
        sendfile on;
        sendfile_max_chunk 512k;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|webp|mp4)$ {
        expires 30d;
    }

    location ~* \.(css|js)$ {
        expires 10d;
    }

    gzip on;
    gzip_vary on;
    gzip_comp_level 4;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_types application/javascript application/json application/x-font-ttf font/opentype image/* text/plain text/css text/xml text/javascript application/x-javascript application/xml;
    gzip_disable "MSIE [1-6]\.";
    gunzip on;

    # error_log /root/nginx-log.txt debug;
}

我的后端服务器将提供具有以下模式的网址:

/basic-url-here     // This style will serve html files built with a templating language from the server, so they need to be at the root path
/api/*

什么是使用 nginx 提供所有这些文件同时还代理到后端服务器的正确/有效方式?

【问题讨论】:

    标签: nginx nginx-reverse-proxy nginx-config


    【解决方案1】:

    我找到了一个可行的解决方案,但我不知道它的效率如何。如果我删除 /asset 位置块,并用这个替换 / 位置块,它可以工作:

    location / {
        try_files $uri $uri/ @backend;
    }
    
    location @backend {
        proxy_pass http://backend:8080;
    }
    

    这是我的最终文件:

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /root/web;
        index index.html;
        server_name _;
    
        access_log off;
        sendfile on;
        sendfile_max_chunk 512k;
    
        location / {
            try_files $uri $uri/ @backend;
        }
    
        location @backend {
            proxy_pass http://backend:8080;
        }
    
        location ~* \.(jpg|jpeg|png|gif|ico|webp|mp4)$ {
            expires 30d;
        }
    
        location ~* \.(css|js)$ {
            expires 10d;
        }
    
        gzip on;
        gzip_vary on;
        gzip_comp_level 4;
        gzip_min_length 256;
        gzip_proxied any;
        gzip_types application/javascript application/json application/x-font-ttf font/opentype image/* text/plain text/css text/xml text/javascript application/x-javascript application/xml;
        gzip_disable "MSIE [1-6]\.";
        gunzip on;
    
        # error_log /root/nginx-log.txt debug;
    }
    

    我不确定这是否是正确的方法。

    【讨论】:

      【解决方案2】:

      您可以使用另一个位置块来映射您的 api,例如,您的 Java 后端服务器将在端口 4000 上运行:

      location /api/ {
          proxy_pass http://localhost:4000:
           ..... <other configurations>
      }
      

      您可以阅读更多关于此以及其他配置的信息in the documentation

      希望有帮助!

      【讨论】:

      • 这并不能真正回答我的问题。我知道我可以为 API 做到这一点,但我还必须为所有这些静态文件提供服务,并且还必须为我的服务器提供不以 /api 为前缀的请求。不过,我感谢您回答我的问题所付出的努力,所以即使您的回答对我没有帮助,我也会给您一个支持,以便您获得一些代表。
      猜你喜欢
      • 2014-07-25
      • 2016-05-23
      • 2023-04-09
      • 1970-01-01
      • 2010-12-01
      • 2015-06-05
      • 2010-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多