本文共 1173 字,大约阅读时间需要 3 分钟。
在本文中,我们将指导您如何在nginx配置中添加一个反向代理路径,并将其重写到实际的API地址http://localhost:1894。
当前nginx配置文件中,仅有基本的站点设置:
server { listen 8094; server_name localhost; # charset koi8-r; # access_log logs/host.access.log main; location / { root html; index index.html index.htm; } # 反向代理设置 location /apis { rewrite ^.+apis/?(.*)$ /$1 break; include uwsgi_params; proxy_pass http://localhost:1894; }} 此外,图1展示了当前配置的完整视图。
修改后的nginx配置文件如下:
server { listen 8094; server_name localhost; # charset koi8-r; # access_log logs/host.access.log main; location / { root html; index index.html index.htm; } # 反向代理设置 location /apis { rewrite ^.+apis/?(.*)$ /$1 break; include uwsgi_params; proxy_pass http://localhost:1894; }} 图2展示了修改后的完整配置文件。
反向代理设置:在location /apis块中,我们使用了rewrite指令将所有请求重写到/,然后将$1参数作为实际请求路径传递。
代理目标地址:proxy_pass指令设置为http://localhost:1894,表示所有到达/apis路径的请求都会被转发到该地址。
UWSGI参数包含:include uwsgi_params;确保所有必要的UWSGI参数都会被传递,必要时可以根据实际需求添加或修改参数。
通过以上配置,您可以实现/apis路径的反向代理请求被重写到真实的API地址http://localhost:1894,从而实现对API资源的正确代理和访问。
转载地址:http://fecfk.baihongyu.com/