在Ubuntu上调试nginx+uwsgi+flask的404错误



我正在寻找帮助,我认为是nginx的配置错误…

uwsgi .ini文件位于/var/www/mysite.com/public_html/api/calc:

[uwsgi]
#application's base folder
base = /var/www/mysite.com/public_html/api/calc
#python module to import
app = hello
module = %(app)
home = %(base)/venv
pythonpath = %(base)
#socket file's location
socket = /var/www/mysite.com/public_html/api/calc/%n.sock
#permissions for the socket file
chmod-socket    = 666
#the variable that holds a flask application inside the module imported at line #6
callable = app
#location of log files
logto = /var/log/uwsgi/%n.log

我的nginx配置使用letsencrypt/certbot服务两个域。mysite.com域也设置为服务于api子域名。相关的nginx服务器块:

server {
server_name api.mysite.com;
root /var/www/mysite.com/public_html/api;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location /calc/ {
include uwsgi_params;
uwsgi_pass unix:/var/www/mysite.com/public_html/api/calc/calc_uwsgi.sock;
}

listen [::]:443 ssl; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

我试图通过添加位置来创建位于api.mysite.com/calc/的API块,指定uwsgi网关定义在/var/www/mysite.com/public_html/api/calc.

在uwsgi运行的情况下,当我访问https://api.mysite.com/calc时,uwsgi日志文件显示我的python正在被调用,并且正在生成响应数据:

[pid: 34683|app: 0|req: 24/24] 192.168.1.1 () {56 vars in 1127 bytes} [Sun Feb 21 18:26:01 2021] GET /calc/ => generated 232 bytes in 2 msecs (HTTP/1.1 404) 2 headers in 87 bytes (1 switches on core 0)

nginx access.log显示访问:

192.168.1.1 - - [21/Feb/2021:18:50:33 -0500] "GET /calc/ HTTP/1.1" 404 209 "-" "Mozilla/5.0 (X11; CrOS x86_64 13505.73.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.109 Safari/537.36"

但是客户端返回一个404 not found错误。在nginx error.log文件中没有条目。

(显然)我不知道我在做什么。我在nginx的配置中尝试了很多不同的东西,但是都不喜欢。

感谢任何建议!

我假设使用这种配置,对app.mysite.com/calc的请求将由"/"路由,但看起来它实际上是由"/calc/"路线:

@app.route("/")
def hello():
return "Hello World - '/' route "
@app.route("/calc/")
def hellocalc():
return "Hello world - '/calc/' route "

是否有一种方法可以配置它,使我的python不需要在其所有路由中包含'calc' ?

我得到这个工作使用内部路由route-uri指令

[uwsgi]
#try rewrite routing
route-uri = ^/calc/?(.*)$ rewrite:/

这需要在安装pcre后重新安装uwsgi:

sudo apt-get install libpcre3 libpcre
pip3 install -I --no-cache-dir  uwsgi

但是这里有另一个问题:我没有用sudo安装uwsgi,所以现在它位于

/home/me/.local/bin/uwsgi

这是好还是坏?

相关内容

最新更新