Nginx为flask应用程序404提供静态文件

我已经尝试了其他职位的无数解决scheme,但似乎没有为我工作,我总是得到一个404错误,当试图让nginx服务我的flask应用程序的静态文件。 在所有这些试图让它运行的尝试之间,我也有一个403 ,但我不能重build它。 这是我目前的设置:

该应用程序位于: ~/development/Python/flask_nginx_gunicorn/ ,应该是一个运行的testing项目,了解nginx等等。 它有以下结构:

项目结构

所以没有什么比一个名为app.py的文件,它在static文件夹中configuration了几条path和一个index.html

我configuration我的nginx如下:

/etc/nginx/nginx.conf包含以下内容:

 user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } 

最后还有一个包含sites-enabled子目录。 在那个目录中我有一个名为flask_project的文件。 在那个文件中,我的项目有以下configuration:

 server { root /home/xiaolong/development/Python/flask_nginx_gunicorn/; server_name localhost; # LISTENING ON WHICH PORTS? listen localhost:7777; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } location /static/ { autoindex on; root /home/xiaolong/development/Python/flask_nginx_gunicorn/; } } 

我使用命令systemctl start nginx 。 当它运行时,我做一个ps -aux | grep nginx ps -aux | grep nginx ,我得到以下输出:

 root 17748 1.2 2.5 1607328 102772 pts/1 Sl 20:45 0:07 /usr/local/sublime_text_3/sublime_text /etc/nginx/nginx.conf root 18762 0.0 0.0 122720 2880 ? Ss 20:53 0:00 nginx: master process /usr/sbin/nginx nginx 18763 0.0 0.1 124808 7916 ? S 20:53 0:00 nginx: worker process nginx 18764 0.0 0.1 124808 7916 ? S 20:53 0:00 nginx: worker process nginx 18765 0.0 0.1 124808 7916 ? S 20:53 0:00 nginx: worker process nginx 18766 0.0 0.1 124808 7916 ? S 20:53 0:00 nginx: worker process root 18988 0.0 0.0 111032 1792 pts/0 S+ 20:54 0:00 tail -f 10 /var/log/nginx/error.log root 19001 0.0 0.0 115820 2396 pts/1 S+ 20:55 0:00 grep --color=auto nginx 

所以我知道它正在运行。 我曾经在日志中得到一个错误,告诉我nginx无法绑定到一个特定的端口,这是由于SELinux只允许某些端口绑定。 我修复了使用命令semanage port --add --type http_port_t --proto tcp 7777

每次更改后,我总是运行systemctl restart nginx ,重新启动nginx服务器,以便它可以重新加载其configuration。

我使用gunicorn app:app -b localhost:8080来运行我的烧瓶应用程序。

当我在浏览器中打开URL http://localhost:8080/static/时,我得到一个404和我使用tail -f 10 /var/log/nginx/error.log读取的日志没有新错误。 烧瓶的应用程序正在运行,我可以validation通过去http://localhost:8080/http://localhost:8080/data ,这两个工作正常。 但是,一旦有静态目录的静态内容,我得到了404

我的猜测是以下几点:

  1. 不知何故nginx并没有被讨论,它不知道静态文件夹内容的请求。
  2. 不知何故path仍然不正确。
  3. 还有一些其他问题,防止它获取浏览器的文件,并使其不logging。

我试图让它运行两天,但没有configuration我find了为我工作。 我也尝试了以下两个在其他地方build议的命令:

  1. 更改目录的安全上下文recursion:

    chcon -Rt httpd_sys_content_t /home/xiaolong/development/Python/flask_nginx_gunicorn/

  2. 允许http守护进程使用networking连接(???)

    setsebool -P httpd_can_network_connect 1

我的configuration有什么问题?

附加信息

在我的/etc/nginx/目录中,我有以下结构:

  • 一个nginx.conf文件
  • 子目录sites-available只包含我的烧瓶应用程序的configuration
  • 子目录sites-enabled包含链接到我的configuration文件的烧瓶应用程序在sites-available

我在这里运行Fedora 22

现在已经有一年多的时间了,所以我确信你已经完成了这个工作,但是如果其他人看到这个,

Nginx正在监听localhost:7777 ,但是你的URL会把你带到你的gunicorn服务器所在的8080端口。 这就是为什么你可以到你的Flask应用程序(由gunicorn服务),而不是你的静态内容(由ngninx服务)。 这也是为什么你没有得到你的nginx日志中的任何东西。 如果你去http://localhost:7777/ ,nginx应该将你的请求传递给你的Flask应用程序, http://localhost:7777/static/应该带你到静态内容。