将所有 /API/. 请求重定向到表达



我目前有一个nginx服务器,运行一个反应应用程序作为前端,一个快速服务器作为后端。

快递服务器运行在本地主机:5000/

server {
listen 80 ;
server_name siteurl;
server_name siteip;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf; # Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
location ^~ /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:5000/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;

}

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
root /usr/share/nginx/site/front;
try_files $uri /index.html =404;
}

我希望所有/api/* 请求都重定向到我的快递服务器,目前当我做 site/api/时它会转到我的快递服务器,但是当做/api/users 时,它不会去我的快递服务器,而是会去我的反应服务器。

我将如何才能完成这项工作 感谢

当你在location指令中使用^~修饰符时,你实际上告诉 nginx 后面的路径是一个正则表达式。如果是这样,则必须将其格式化为正则表达式:

location ^~ /api/.* {

最新更新