nginx最简易的配置,转发所有
-
需求
阿里云ec2上起了个python Fastapi写的服务。现需要用阿里云的公网ip来访问所有api。服务器本地服务用的是5001端口。
-
问题
起初发现通过公网ip只能访问到/
其他路径都404
将nginx location下 proxy_pass http://127.0.0.1:5001$request_uri; -
解决方法
将nginx location下 proxy_pass http://127.0.0.1:5001; 改为http://127.0.0.1:5001$request_uri;
nginx配置文件如下
upstream flask {
server 127.0.0.1:5001;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_pass http://127.0.0.1:5001$request_uri;
proxy_set_header Host $host;
# 设置请求头,传递原始请求ip给 gunicorn 服务器
proxy_set_header X-Real-IP $remote_addr;
try_files $uri $uri/ =404;
}
}