Flask on nginx + uWSGI returns a 404 error unless the linux directory exists

主宰稳场 提交于 2021-02-10 06:34:26

问题


This might be kind of a strange problem, but I'm not too experienced with these things and I don't know how to search for this kind of error.

I have a server configured with nginx and uWSGI. Everything runs fine, no errors in the logs that I can see. However, when I'm executing the below code:

from flask import Flask
app = Flask(__name__)

@app.route('/test/')
def page1():
    return 'Hello World'

@app.route('/')
def index():
    return 'Index Page'

I can not view http://ezte.ch/test/ UNLESS the /test/ directory exists inside linux once I create that directory, everything loads fine. Otherwise I get a 404 error passed to the uWSGI (it does show that it's receiving the request in the terminal) process.

Here is my config.ini for uWSGI:

[uwsgi]
project = eztech

uid = www-data
gid = www-data
plugins = http,python

socket = /usr/share/nginx/www/eztech/uwsgi.sock



chmod-socket = 775
chown-socket = www-data:www-data
wsgi-file hello.py
callable app
processes 4
threads 2

Here is my nginx configuration:

            server {
    listen   80; ## listen for ipv4; this line is default and implied
    listen   [::]:80 default ipv6only=on; ## listen for ipv6
    autoindex  on;
    root /usr/share/nginx/www/eztech/public_html;
    index index.html index.htm;
    # Make site accessible from http://localhost/
    server_name ezte.ch;
    location  / {
            uwsgi_pass unix:/usr/share/nginx/www/eztech/uwsgi.sock;
            include uwsgi_params;
            uwsgi_param         UWSGI_CHDIR           /usr/share/nginx/www/eztech/public_html;
            uwsgi_param         UWSGI_MODULE    hello;
            uwsgi_param         UWSGI_CALLABLE  app;
            # First attempt to serve request as file, then
            # as directory, then fall back to index.html
            try_files $uri $uri/ /index.html;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

Below is what I get when running uWSGI with my config file:

   [uWSGI] getting INI configuration from config.ini
   open("./http_plugin.so"): No such file or directory [core/utils.c line 3347]
   !!! UNABLE to load uWSGI plugin: ./http_plugin.so: cannot open shared object file: No such    file or directory !!!
   open("./python_plugin.so"): No such file or directory [core/utils.c line 3347]
   !!! UNABLE to load uWSGI plugin: ./python_plugin.so: cannot open shared object file: No       such file or directory !!!
   *** Starting uWSGI 1.9.8 (64bit) on [Sat Apr 27 06:29:18 2013] ***
   compiled with version: 4.6.3 on 27 April 2013 00:06:22
   os: Linux-3.2.0-36-virtual #57-Ubuntu SMP Tue Jan 8 22:04:49 UTC 2013
   nodename: ip-10-245-51-230
   machine: x86_64
   clock source: unix
   detected number of CPU cores: 1
   current working directory: /usr/share/nginx/www/eztech
   detected binary path: /usr/local/bin/uwsgi
   *** WARNING: you are running uWSGI without its master process manager ***
   your processes number limit is 4595
   your memory page size is 4096 bytes
   detected max file descriptor number: 1024
   lock engine: pthread robust mutexes
   uwsgi socket 0 bound to UNIX address /usr/share/nginx/www/eztech/uwsgi.sock fd 3
   setgid() to 33
   setuid() to 33
   Python version: 2.7.3 (default, Aug  1 2012, 05:25:23)  [GCC 4.6.3]
   *** Python threads support is disabled. You can enable it with --enable-threads ***
   Python main interpreter initialized at 0x2505520
   your server socket listen backlog is limited to 100 connections
   your mercy for graceful operations on workers is 60 seconds
   mapped 72688 bytes (70 KB) for 1 cores
   *** Operational MODE: single process ***
   *** no app loaded. going in full dynamic mode ***
   *** uWSGI is running in multiple interpreter mode ***
   spawned uWSGI worker 1 (and the only) (pid: 12800, cores: 1)

Thank you for any assistance you can offer!


回答1:


As Blender already says, there should be no try_files where is your upstream called.

The following nginx config is enough to host flask application:

server {
    listen 80;
    server_name ezte.ch;

    location / {
        uwsgi_pass unix:/usr/share/nginx/www/eztech/uwsgi.sock;
        include uwsgi_params;
    }
}

my flask config:

<uwsgi>
    <autostart>true</autostart>
    <master/>
    <pythonpath>/var/www/apps/someapp/</pythonpath>
    <plugin>python</plugin>
    <module>someapp:app</module>
    <processes>4</processes>
</uwsgi>

So there is path /var/www/apps/someapp/ and flask file someapp.py




回答2:


I had the same issue. just remove this line from the nginx configuration :

root /usr/share/nginx/www/eztech/public_html;


来源:https://stackoverflow.com/questions/16249054/flask-on-nginx-uwsgi-returns-a-404-error-unless-the-linux-directory-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!