V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
diangdiang
V2EX  ›  程序员

小白求助 Django+uWSGI+nginx 部署时,通过 uWSGI 的端口的可以访问, Nginx 404,在线等大佬

  •  
  •   diangdiang · 2019-02-16 16:31:10 +08:00 · 4827 次点击
    这是一个创建于 1867 天前的主题,其中的信息可能已经有所发展或是发生改变。

    是哪里配置的不对吗,折腾了好久,等大佬帮忙~

    mysite_nginx.conf 文件

    # the upstream component nginx needs to connect to
    upstream django {
        server unix:///home/lee/code/toyinstagram/bookmarks/mysite.sock; # for a file socket
        # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
    }
    
    # configuration of the server
    server {
        # the port your site will be served on
        listen      9310;
        # the domain name it will serve for
        server_name 127.0.0.1; # substitute your machine's IP address or FQDN
        charset     utf-8;
    
        # max upload size
        client_max_body_size 75M;   # adjust to taste
    
        # Django media
        location /media  {
            alias /home/lee/code/toyinstagram/bookmarks/media;  # your Django project's media files - amend as required
        }
    
        location /static {
            alias /home/lee/code/toyinstagram/bookmarks/account/static; # your Django project's static files - amend as required
        }
    
        # Finally, send all non-media requests to the Django server.
        location / {
            uwsgi_pass  django;
            include     /home/lee/code/toyinstagram/bookmarks/uwsgi_params; # the uwsgi_params file you installed
        }
    }
    

    uWSGI

    uwsgi.ini 文件

    [uwsgi]
    http = :9310
    # the local unix socket file than commnuincate to Nginx
    socket = /home/lee/code/toyinstagram/bookmarks/mysite.sock
    # the base directory (full path)
    chdir = /home/lee/code/toyinstagram/bookmarks
    # Django's wsgi file
    wsgi-file = bookmarks/wsgi.py
    # maximum number of worker processes
    processes = 4
    #thread numbers startched in each worker process
    threads = 2
     
    #monitor uwsgi status
    stats = 127.0.0.1:9191
    # clear environment on exit
    vacuum          = true
    

    80 端口 404




    9310 端口 可正常访问

    第 1 条附言  ·  2019-02-16 18:10:56 +08:00
    • Nginx和uWSGI分别监听 80和 9310端口
    第 2 条附言  ·  2019-02-16 23:00:37 +08:00

    最新配置, 大家帮忙看一下还有什么问题 🤣,目前还是不行啊

    • uwsgi.ini
    [uwsgi]
    # http  = :9310
    # the local unix socket file than commnuincate to Nginx
    socket = :9310
    # the base directory (full path)
    chdir = /home/lee/code/toyinstagram/bookmarks
    # Django's wsgi file
    wsgi-file = bookmarks/wsgi.py
    # maximum number of worker processes
    processes = 4
    #thread numbers startched in each worker process
    threads = 2
     
    #monitor uwsgi status
    stats = 127.0.0.1:9191
    # clear environment on exit
    vacuum          = true
    
    • mysite_nginx.conf
    upstream django {
        # server unix:///home/lee/code/toyinstagram/bookmarks/mysite.sock; # for a file socket
        server 127.0.0.1:9310; # for a web port socket (we'll use this first)
    }
    
    # configuration of the server
    server {
        # the port your site will be served on
        listen  80;
        # the domain name it will serve for
        server_name 152.136.39.150; # substitute your machine's IP address or FQDN
        charset     utf-8;
    
        # max upload size
        client_max_body_size 75M;   # adjust to taste
    
        # Django media
        location /media  {
            alias /home/lee/code/toyinstagram/bookmarks/media;  # your Django project's media files - amend as required
        }
    
        location /static {
            alias /home/lee/code/toyinstagram/bookmarks/static; # your Django project's static files - amend as required
        }
    
        # Finally, send all non-media requests to the Django server.
        location / {
            uwsgi_pass  diango; 
            include     /home/lee/code/toyinstagram/bookmarks/uwsgi_params; # the uwsgi_params file you installed
        }
        error_log /var/log/nginx/nginx_error.log;
       
    }
    
    46 条回复    2019-02-17 16:31:18 +08:00
    diangdiang
        1
    diangdiang  
    OP
       2019-02-16 16:32:02 +08:00
    求别沉,卡了好久了
    746970179
        2
    746970179  
       2019-02-16 16:36:46 +08:00
    好像 nginx 中的 listen 的端口, 就是 nginx 的端口
    你要是想访问 80, nginx 的 listen 端口改成 80, 然后重启 nginx 试一下呢
    blless
        3
    blless  
       2019-02-16 16:44:51 +08:00 via Android
    nginx 版本这么老…那个 80 可能是默认配置文件监听的吧?检查一下 nginx 跟 uwsgi 的日志先?我怀疑你这 nginx 压根没用上
    diangdiang
        4
    diangdiang  
    OP
       2019-02-16 16:46:47 +08:00
    @746970179 不行呢,按照上面的配置文件,nginx 监听是 80 端口
    是不是 client 通过 80 端口 访问 nginx,nginx 通过 9310 和 uWSGI 通信,这块一直没搞太清楚
    <img src="https://i.loli.net/2019/02/16/5c67cdd8c3a30.jpg">
    diangdiang
        5
    diangdiang  
    OP
       2019-02-16 16:48:11 +08:00
    @blless 请求过来的时候 nginx 有日志输出,但是 uwsgi 控制台没有,感觉是请求 到了 nginx,但是 没到 uwsgi
    wukui
        6
    wukui  
       2019-02-16 16:51:24 +08:00
    nginx 配置文件中的 server_name 127.0.0.1; 应该只能在 nginx 运行的机器上访问 http://127.0.0.1:9310/ 。 如果你想在除了 nginx 运行机器以外打开,需要在 server_name 127.0.0.1; 这个配置加上你服务器 IP 或者域名。 不知道是不是你要的答案。
    diangdiang
        7
    diangdiang  
    OP
       2019-02-16 16:53:55 +08:00
    @wukui 改成服务器自己的 ip 还是不行🤣
    neoblackcap
        8
    neoblackcap  
       2019-02-16 16:57:25 +08:00
    两个服务的端口都重复了,你将 nginx 配置里面的 listen 改为 80 吧
    diangdiang
        9
    diangdiang  
    OP
       2019-02-16 17:01:36 +08:00
    @neoblackcap 改成 80 了 还是 404,请求的时候 nginx 有日志,但是 uwsgi 控制台没有🤣
    diangdiang
        10
    diangdiang  
    OP
       2019-02-16 17:06:26 +08:00
    @neoblackcap 感觉是不是 请求到了 nginx,但是 nginx 和 uwsgi 通信 出了问题,没到 uwsgi,uwsgi 控制台也没有相关的输出
    diangdiang
        11
    diangdiang  
    OP
       2019-02-16 17:08:21 +08:00
    @neoblackcap 单独访问 9310 的端口,可以通过 uwsgi 正常访问到,uwsgi 控制台也有输出🤣
    NeverBelieveMe
        12
    NeverBelieveMe  
       2019-02-16 17:29:50 +08:00
    你查一下 nginx+uwsgi 配置。你这个 nginx 配置不对。
    NeverBelieveMe
        13
    NeverBelieveMe  
       2019-02-16 17:31:20 +08:00
    server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    root /usr/share/nginx/html;

    location /{
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:5000;

    }

    }
    NeverBelieveMe
        14
    NeverBelieveMe  
       2019-02-16 17:32:06 +08:00
    上面是 nginx+uwsgi+flask 的配置的时候 nginx 的配置
    ytmsdy
        15
    ytmsdy  
       2019-02-16 17:33:53 +08:00
    # the port your site will be served on
    listen 9310;

    这里应该是 80 吧。。。。
    你试试本地的 9310 端口能不能访问。
    zqyisasd
        16
    zqyisasd  
       2019-02-16 17:36:27 +08:00   ❤️ 1
    我的配置 django.conf
    upstream django {
    server 192.168.115.21:8111;
    }
    server {
    listen 80;
    server_name 192.168.115.21;
    location / {
    uwsgi_pass unix:/usr/share/nginx/django_monitor/uwsgi.sock;
    include /etc/nginx/uwsgi_params;
    index /ad/login/;
    }
    }
    uwsgi.ini
    [uwsgi]
    socket = 0.0.0.0:8111
    chdir = /usr/share/nginx/django_monitor/monitor/
    module = monitor.wsgi
    master = true
    processes = 1
    threads=2
    chmod-socket = 666
    vacuum = true
    static-map=/static=/usr/share/nginx/django_monitor/monitor/static
    uid=nginx
    gid=nginx
    socket=/usr/share/nginx/django_monitor/uwsgi.sock
    enable-threads = true
    # 序列化接受的内容,如果可能的话
    thunder-lock=true
    # 设置自中断时间
    harakiri=30
    # 设置缓冲
    post-buffering=1024
    max-requests = 1024
    diangdiang
        17
    diangdiang  
    OP
       2019-02-16 17:39:46 +08:00
    @ytmsdy 改成 80 了也不行 🤣
    Humorce
        18
    Humorce  
       2019-02-16 17:45:56 +08:00 via iPhone   ❤️ 1
    那么为什么不贴 nginx 的日志呢
    JoyBanana
        19
    JoyBanana  
       2019-02-16 17:51:23 +08:00
    没有人监听 80 端口,nginx 报 404 很正常吧。uWSGI 和 nginx 都监听 9310 吗?
    diangdiang
        20
    diangdiang  
    OP
       2019-02-16 17:51:41 +08:00
    @zqyisasd
    Hi 老哥,想问一下, 8111 是配置 uwsgi 前,调试 django 用的接口吗
    另外 uwsgi.ini 文件的 socket 的值 是不是被覆盖了🤣
    <img src="https://i.loli.net/2019/02/16/5c67dc9541cdd.jpg">
    diangdiang
        21
    diangdiang  
    OP
       2019-02-16 17:59:57 +08:00
    @JoyBanana nginx 有监听 80 端口, uwsgi 监听 9310 端口
    <img src="https://i.loli.net/2019/02/16/5c67defbae0c0.jpg">
    diangdiang
        22
    diangdiang  
    OP
       2019-02-16 18:00:31 +08:00
    回复里不能贴图吗,是不是贴图方式不对
    zqyisasd
        23
    zqyisasd  
       2019-02-16 18:12:45 +08:00
    差不多是,不过 socket 不能直接访问,要改成 http 才能直接访问; ini 里面的是 socket,nginx 里面表示代理到这个 socket,不会覆盖。
    qqxx520
        24
    qqxx520  
       2019-02-16 18:18:51 +08:00 via iPhone
    把 http=:9310 这行去掉再试试
    defunct9
        25
    defunct9  
       2019-02-16 18:23:30 +08:00 via iPhone
    开 ssh,让我上去看看
    diangdiang
        26
    diangdiang  
    OP
       2019-02-16 18:24:29 +08:00
    @qqxx520 不行啊 老哥🤣,貌似
    ```
    http = :9310 和 socket = 0.0.0.0:9310 得有一个才行
    allenjuly7
        27
    allenjuly7  
       2019-02-16 18:56:32 +08:00
    qqxx520
        28
    qqxx520  
       2019-02-16 18:59:19 +08:00   ❤️ 1
    你已经在 nginx 里用了 unix socket,因此 uwsgi 里要启用 unix socket,不能在用基于 ip 的 socket, 用 unix socket 比较麻烦的一点是,它其实是一个文件,要注意写权限。
    diangdiang
        29
    diangdiang  
    OP
       2019-02-16 19:24:11 +08:00
    @qqxx520
    uwsgi.ini 和 nginx.conf 里 都用基于 ip 的 socket 可以吗,试了好像还是不行 23333,
    都用基于 ip 的 socket 有什么配置要求吗,有没有例子之类的。

    - mysite_nginx.conf
    ```
    # the upstream component nginx needs to connect to
    upstream django {
    # server unix:///home/lee/code/toyinstagram/bookmarks/mysite.sock; # for a file socket
    server http://127.0.0.1:9310; # for a web port socket (we'll use this first)
    }

    # configuration of the server
    server {
    # the port your site will be served on
    listen 80;
    # the domain name it will serve for
    server_name 127.0.0.1; # substitute your machine's IP address or FQDN
    charset utf-8;

    # max upload size
    client_max_body_size 75M; # adjust to taste

    # Django media
    location /media {
    alias /home/lee/code/toyinstagram/bookmarks/media; # your Django project's media files - amend as required
    }

    location /static {
    alias /home/lee/code/toyinstagram/bookmarks/account/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
    uwsgi_pass django;
    include /home/lee/code/toyinstagram/bookmarks/uwsgi_params; # the uwsgi_params file you installed
    }
    }
    ```

    usgi.ini
    ```
    [uwsgi]
    http = :9310
    # the local unix socket file than commnuincate to Nginx
    # socket = 0.0.0.0:9310
    # the base directory (full path)
    chdir = /home/lee/code/toyinstagram/bookmarks
    # Django's wsgi file
    wsgi-file = bookmarks/wsgi.py
    # maximum number of worker processes
    processes = 4
    #thread numbers startched in each worker process
    threads = 2

    #monitor uwsgi status
    stats = 127.0.0.1:9191
    # clear environment on exit
    vacuum = true
    qqxx520
        30
    qqxx520  
       2019-02-16 20:05:01 +08:00
    @diangdiang
    改动两行试试:
    # http = :9310
    socket = 127.0.0.1:9310
    shaoS
        31
    shaoS  
       2019-02-16 20:09:01 +08:00
    看下 nginx 的 error log,有没有 socket 文件权限的问题
    diangdiang
        32
    diangdiang  
    OP
       2019-02-16 20:12:44 +08:00
    @qqxx520 还是不行 感觉一直是 nginx 和 uWSGI 没有通信,
    我再看看吧,多谢老哥
    diangdiang
        33
    diangdiang  
    OP
       2019-02-16 20:13:45 +08:00
    @shaoS error.log 是空的
    shaoS
        34
    shaoS  
       2019-02-16 20:15:28 +08:00
    @diangdiang 你就加下 和 access_log 写一起 error_log /var/log/nginx/error.log;
    diangdiang
        35
    diangdiang  
    OP
       2019-02-16 20:18:56 +08:00
    @shaoS 啥意思 老哥 感觉不是没有写权限的问题 access.log 有输出
    diangdiang
        36
    diangdiang  
    OP
       2019-02-16 20:21:39 +08:00
    多谢各位 热心回复,我自己看一下吧,应该就是配置的问题,
    shaoS
        37
    shaoS  
       2019-02-16 20:22:50 +08:00
    @diangdiang access.log 只是输出了 404 的 get 请求,你需要看下 error.log 具体报了错误什么,error_log 这个参数需要配置上 nginx 才会打印 error.log ,你还是看下 nginx 的一些知识吧,你可以在 usgi.ini 加一行 chmod-socket=777 试下
    diangdiang
        38
    diangdiang  
    OP
       2019-02-16 20:41:47 +08:00
    @shaoS 老哥帮忙看下这是咋回事
    /var/nginx/error.log

    2019/02/16 20:39:52 [notice] 4541#0: signal process started



    /var/nginx/access.log

    223.72.80.157 - - [16/Feb/2019:20:39:59 +0800] "GET /account/ HTTP/1.1" 404 200 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
    Lax
        39
    Lax  
       2019-02-16 20:51:54 +08:00
    这两行配合起来,其它地方先不用动:

    listen 80;
    server_name xxx.xxx.xxx.150; #浏览器里用的网址 /IP
    shaoS
        40
    shaoS  
       2019-02-16 21:01:29 +08:00
    @diangdiang signal process started 这个错误应该是你的 nginx 已经运行中,重复启动 nginx 会报这个错误,这个错误和 404 应该没什么关系,如果后端需要用 socket 方式需要注意文件权限问题,你可以看下 uwsgi 的 chown-socket 和 chmod-socket 2 个参数,不行你就用 http 的方式
    diangdiang
        41
    diangdiang  
    OP
       2019-02-16 21:20:11 +08:00
    @Lax 嗯 目前是这样配的 还是 有问题
    mysite_nginx.conf 里的
    location / {
    uwsgi_pass django; # 这里是不是有问题,我看有的不是这样配的,但是换成 127.0.0.1:uwsgi 的端口 也不行诶
    include /home/lee/code/toyinstagram/bookmarks/uwsgi_params; # the uwsgi_params file you installed
    }
    cepro
        42
    cepro  
       2019-02-16 21:50:35 +08:00   ❤️ 1
    1.权限问题。
    2.重启 nginx。
    3.检查一下自己的路径。(有时候多一个"/"或者少一个就不一样了)
    4.setting 文件里面的 DEBUG 和 HOST 是否正确。

    可以通过看 nginx 的错误日志来分析分析。
    ushuz
        43
    ushuz  
       2019-02-16 22:26:12 +08:00
    你的 uwsgi 监听端口用的是 http 协议,nginx 反代应该用 `proxy_pass`。如果要用 `uwsgi_pass`,那 uwsgi 配置应该把 `http = :9310` 换成 `socket = :9310`
    diangdiang
        44
    diangdiang  
    OP
       2019-02-16 23:01:44 +08:00
    @ushuz 嗯 这里改了, 老哥帮忙看下一下 主题附言 部分的 最新配置文件还有什么问题吗 还是不行诶
    theks
        45
    theks  
       2019-02-17 11:50:28 +08:00 via Android
    我没仔细看你的需求描述,感觉上应该是你的 django 程序已经正常运行在 9310 端口,只需要 nginx 做个反向代理就行。
    server {
    listen 80;
    #server_name 152.136.39.150; #这里写你自己的域名或者 ip 或者其它主机头
    location / {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://127.0.0.1:9310;
    }

    }
    BooksE
        46
    BooksE  
       2019-02-17 16:31:18 +08:00
    先看 nginx 的 log,我以前出现类似情况( uwsgi 通,nginx 不通)是因为防火墙的问题,把防火墙关了就成功了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   958 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 20:42 · PVG 04:42 · LAX 13:42 · JFK 16:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.