【发布时间】:2021-05-24 06:25:13
【问题描述】:
我正在为我认为是 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 {
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
}
我正在尝试通过添加一个 location 块来创建位于 api.mysite.com/calc/ 的 API,该块指定 /var/www/mysite.com/public_html 中定义的 uwsgi 网关/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 的请求将由我的 python 中的“/”路由提供服务,但看起来它实际上由“/calc/”路由提供服务:
@app.route("/")
def hello():
return "Hello World - '/' route "
@app.route("/calc/")
def hellocalc():
return "Hello world - '/calc/' route "
有没有办法配置它,以便我的 python 不需要在其所有路由中包含“calc”?
【问题讨论】: