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

2019-02-16 16:31:10 +08:00
 diangdiang

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

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 端口 可正常访问

4879 次点击
所在节点    程序员
46 条回复
diangdiang
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
2019-02-16 18:00:31 +08:00
回复里不能贴图吗,是不是贴图方式不对
zqyisasd
2019-02-16 18:12:45 +08:00
差不多是,不过 socket 不能直接访问,要改成 http 才能直接访问; ini 里面的是 socket,nginx 里面表示代理到这个 socket,不会覆盖。
qqxx520
2019-02-16 18:18:51 +08:00
把 http=:9310 这行去掉再试试
defunct9
2019-02-16 18:23:30 +08:00
开 ssh,让我上去看看
diangdiang
2019-02-16 18:24:29 +08:00
@qqxx520 不行啊 老哥🤣,貌似
```
http = :9310 和 socket = 0.0.0.0:9310 得有一个才行
allenjuly7
2019-02-16 18:56:32 +08:00
qqxx520
2019-02-16 18:59:19 +08:00
你已经在 nginx 里用了 unix socket,因此 uwsgi 里要启用 unix socket,不能在用基于 ip 的 socket, 用 unix socket 比较麻烦的一点是,它其实是一个文件,要注意写权限。
diangdiang
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
2019-02-16 20:05:01 +08:00
@diangdiang
改动两行试试:
# http = :9310
socket = 127.0.0.1:9310
shaoS
2019-02-16 20:09:01 +08:00
看下 nginx 的 error log,有没有 socket 文件权限的问题
diangdiang
2019-02-16 20:12:44 +08:00
@qqxx520 还是不行 感觉一直是 nginx 和 uWSGI 没有通信,
我再看看吧,多谢老哥
diangdiang
2019-02-16 20:13:45 +08:00
@shaoS error.log 是空的
shaoS
2019-02-16 20:15:28 +08:00
@diangdiang 你就加下 和 access_log 写一起 error_log /var/log/nginx/error.log;
diangdiang
2019-02-16 20:18:56 +08:00
@shaoS 啥意思 老哥 感觉不是没有写权限的问题 access.log 有输出
diangdiang
2019-02-16 20:21:39 +08:00
多谢各位 热心回复,我自己看一下吧,应该就是配置的问题,
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
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
2019-02-16 20:51:54 +08:00
这两行配合起来,其它地方先不用动:

listen 80;
server_name xxx.xxx.xxx.150; #浏览器里用的网址 /IP
shaoS
2019-02-16 21:01:29 +08:00
@diangdiang signal process started 这个错误应该是你的 nginx 已经运行中,重复启动 nginx 会报这个错误,这个错误和 404 应该没什么关系,如果后端需要用 socket 方式需要注意文件权限问题,你可以看下 uwsgi 的 chown-socket 和 chmod-socket 2 个参数,不行你就用 http 的方式

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/535647

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX