bottle uwsgi nginx app returns 404

2019-09-01 02:07发布

问题:

OK, I've gone around and around on this and don't know what else to do: maybe someone can help. I'm trying to run a bottle app via uwsgi and nginx. It works fine if I run it with the bottle server, like this; python app.py and then point a browser to hostname:8080 but when run via nginx/uwsgi, and go to: hostname/apps/app1 I get the 404 error handler WITHIN THE APP, which reports a 404 error. So it's running the app: it just doesn't seem to match any of the

route "decorators". Here's the app code, which resides in /mnt/wd/www/dev/apps/app1/app.py:

import bottle
import os

bottle.debug(True)

app = bottle.Bottle()

@app.route('/')
def default():
  return 'app1 (bottle): I am here. '

@app.route('/hello')
@app.route('/hello/')
@app.route('/hello/<name>')
def greet(name='Stranger'):
  return 'Hello, %s, how are you?' % name

@app.route('/show')
@app.route('/show/<input>')
def show(input=''):
  return 'You entered: %s' % input

@app.error(404)
def error404(error):
   return '<strong>app1: 404 error returned:</strong> %s' %error

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 8080))

app.run(host='0.0.0.0', port=port, debug=True)

Here's my uwsgi ini file:

[uwsgi]
socket = /run/uwsgi/app/app1/socket
chmod-socket = 777 
chdir = /mnt/wd/www/dev/apps/app1/
file = app.py
callable = app
master = true
uid = www-data

gid = www-data

and here is the relevant part of the nginx site file:

    location /apps {
   #    try_files @uri @uwsgi;
    }

    location /apps/app1 {
      include uwsgi_params;
      access_log /var/log/nginx/app1-access.log;
      error_log /var/log/nginx/app1-error.log;
      uwsgi_pass unix:/run/uwsgi/app/app1/socket;

}

So: what can you tell me? Thanks for any help you can provide. Even something to tell me what the app thinks it's getting as the url would be helpful.

回答1:

You have configured nginx to answer on /apps/app1, so for the bottle point of view your requests are /apps/app1/hello... instead of /hello

No brainer solution:

mount = /apps/app1=app.py
manage-script-name = true

and remove the 'file' directive in uwsgi.ini

If you want to understand why WSGI works in this way, you can check how SCRIPT_NAME and PATH_INFO are managed