【问题标题】:nginx serving static files from root and uploaded files from aliasnginx 从根服务静态文件并从别名上传文件
【发布时间】:2017-05-23 03:00:53
【问题描述】:

我在 apache 前面运行 nginx 作为反向代理服务器。 我需要从后端的前端访问上传的文件,所以要走的路是在 nginx 站点配置中使用别名,但后端的静态文件应该由 nginx 直接处理。我是 nginx 新手,所以这是我处理静态文件的部分配置。我还指定了一个别名 (/images),但它不起作用,因为它被第二个条件覆盖。 如何将这两个条件结合起来,以便 nginx 处理来自根(后端应用程序)的静态文件和从前端应用程序上传的文件。 在 apache 配置中,我为这个问题添加了一个别名,它可以工作,但前面没有 nginx。

这是我的部分 nginx 配置:

  server {
       listen 80;

       root /var/www/website/backend/www;

       # Add index.php to the list if you are using PHP
       index index.html index.php index.htm;

       server_name admin.website.com www.admin.website.com;

       location / {
                proxy_pass http://localhost:8080;
                include /etc/nginx/proxy_params;
       }
#The alias to handle uploaded files(.jpeg, .pdf) from frontend
      location  /images {
               alias /var/www/website/frontend/www/images;
      }
#let nginx handle static files from root
       location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
               expires      30d;
       }
.
.
.
}

【问题讨论】:

    标签: nginx


    【解决方案1】:

    正则表达式location 块优先于前缀location 块(除非使用^~ 修饰符)。详情请见this document。

    试试:

    location ^~ /images {
        root /var/www/website/frontend/www;
    }
    

    请注意,在这种情况下首选 root 指令(有关详细信息,请参阅 this document)

    【讨论】:

      猜你喜欢
      • 2012-05-24
      • 1970-01-01
      • 2016-04-05
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 2016-05-23
      相关资源
      最近更新 更多