【问题标题】:Using try_files with uwsgi将 try_files 与 uwsgi 一起使用
【发布时间】:2016-07-07 16:07:47
【问题描述】:

我正在尝试将 nginx try_files 指令与 uwsgi_pass 一起使用,但遇到了很多困难。

基本上,我想要try_files 询问 uWSGI 容器请求 URI 是否有效,如果不是,则提供 index.html 文件。我的 nginx 配置如下:

server {
   listen 80;
   access_log /tmp/nginx.log;

   location / {
       try_files $uri $uri/ /index.html;
       include uwsgi_params;
       uwsgi_pass 127.0.0.1:5001;
   }
}

但它的作用是检查每个请求的 docroot,如果不存在,它会简单地放弃并返回 index.html 文件。

我想要的是以下内容:

  1. 请求进入 www.myapp.com
  2. nginx 将此请求转发到 uWSGI 容器上
  3. 如果无效,则返回 index.html

有没有办法让 uWSGI 改为尝试这些文件?

我最终要在这里完成的是带有 React Router 的 HTML5 Pushstate。我正在运行一个带有 React 前端的 Flask 应用程序。如果用户在www.myapp.com/preferences/userid 刷新浏览器,那么我希望 nginx 将其转发到容器,如果它无效,则返回索引。

【问题讨论】:

  • 你不能为 uwsgi 处理的请求使用单独的路径(使用 2 个位置/总是返回索引 html。/api 总是传递到 uwsgi)
  • @Chamindu 我已经尝试过了,但问题是我的 index.html 文件也由 uWSGI 提供。但也许这是我问题的根源......
  • 我觉得最好让nginx服务静态文件,让uwsgi去管动态内容。

标签: nginx flask react-router uwsgi


【解决方案1】:

所以,在与@Chamindu 交谈后,我意识到我可能以错误的方式处理这件事。我阻止 uWSGI 为我的 index.html 提供服务(即使它可以),而是依靠 nginx 来提供服务。

server {
   listen 80;
   access_log /tmp/nginx.log;

   location / {
       root /var/www/myapplication/;
       try_files $uri $uri/ /index.html;
   }

   location /api {
       include uwsgi_params;
       uwsgi_pass 127.0.0.1:5001;
   }
}

【讨论】:

    猜你喜欢
    • 2018-10-23
    • 2015-12-06
    • 1970-01-01
    • 2015-04-25
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 2020-03-23
    相关资源
    最近更新 更多