V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
codeface
14.49D
V2EX  ›  问与答

Nginx+uWSGI+Flask 配置失败,求教

  •  
  •   codeface · Jan 10, 2016 · 3158 views
    This topic created in 3763 days ago, the information mentioned may be changed or developed.

    服务器为 centOS 7.0
    Python 版本为 3.5
    uWSGI 版本为 2.0.12
    Ngnix 版本为 1.9.9
    Flask 也是最新版

    按下面的配置运行后,访问首页能正常显示 Hello world ,但访问 /about 显示 404 。

    app.py 内容如下

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route("/")
    def index():
        return("Hello world")
    
    @app.route("/about")
    def about():
        return("this is a test")
    
    if __name__ == "__main__":
        app.run()
    

    uwsgi.ini 如下

    [uwsgi]
    # uwsgi 启动时所使用的地址与端口
    socket = 127.0.0.1:3031
    chdir = /home/www/app
    wsgi-file = app.py
    callable = app
    # 处理器数
    processes = 4
    # 线程数
    threads = 2
    #状态检测地址
    stats = 127.0.0.1:9191
    #守护进程
    daemonize = /var/log/uwsgi/uwsgi.log
    

    /usr/local/nginx/conf/nginx.conf 如下:

    events {
        worker_connections  1024;
    }
    
    
    http {
        include       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  logs/access.log  main;
        sendfile        on;
        keepalive_timeout  65;
    
        server {
            listen       80;
            server_name  127.0.0.1;
    
            #charset koi8-r;
    
            access_log  logs/host.access.log  main;
    
            location / {
                include uwsgi_params;
                uwsgi_pass 127.0.0.1:3031;
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    

    自己测试修改,觉得应该是 ngnix 的配置问题。
    搜了很多资料,有提到修改 /etc/nginx/sites-enabled/default 这个文件的,但我没有找到这个路径。

    谁能帮我看看,不胜感激。

    3 replies    2016-01-19 19:42:44 +08:00
    codeface
        1
    codeface  
    OP
       Jan 10, 2016
    [pid: 14935|app: 0|req: 78/128] 220.180.56.180 () {48 vars in 787 bytes} [Sun Jan 10 14:12:09 2016] GET / => generated 14 bytes in 0 msecs (HTTP/1.1 200) 2 headers in 79 bytes (1 switches on core 0)

    [pid: 14935|app: 0|req: 79/129] 220.180.56.180 () {46 vars in 766 bytes} [Sun Jan 10 14:12:13 2016] GET /about => generated 233 bytes in 1 msecs (HTTP/1.1 404) 2 headers in 72 bytes (1 switches on core 0)

    这是 uwsgi 打出来的 log
    codeface
        2
    codeface  
    OP
       Jan 10, 2016
    貌似找到原因了。
    修改 uwsgi 的配置文件后,
    使用 /usr/local/python3/bin/uwsgi --reload /home/codeface/app/uwsgi_conf.ini 重新加载配置文件并没有起作用。
    kill 掉 uwsgi 的进程,重新启动就好了。
    qq3102328040
        3
    qq3102328040  
       Jan 19, 2016   ❤️ 1
    受人之托,来挽个尊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4708 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 09:39 · PVG 17:39 · LAX 02:39 · JFK 05:39
    ♥ Do have faith in what you're doing.