如何在Nginx中重定向特定路径请求至Stripe Checkout?
问题背景与需求
- 前端:Mac环境下React开发,启动命令:
sudo PORT=8000 HTTPS=true SSL_CRT_FILE=ssl/localhost-mac/cert.pem SSL_KEY_FILE=ssl/localhost-mac/key.pem ./node_modules/.bin/react-scripts start,首页访问地址为https://localhost:8000/#/home - 后端:Mac环境下独立文件夹执行
yarn start启动,服务地址https://localhost:3000,当前前后端配合正常 - 当前Nginx配置(仅443端口server块):
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format my_log '{ "time": "$time_iso8601", ' '"remote_addr": "$remote_addr", ' '"status": "$status", ' '"request": "$request", ' '"request_method": "$request_method", ' '"http_referrer": "$http_referer", ' '"http_x_forwarded_for": "$http_x_forwarded_for", ' '"host": "$host", ' '"server_name": "$server_name", ' '"upstream_address": "$upstream_addr", ' '"upstream_status": "$upstream_status" }'; access_log /usr/local/var/log/nginx/access.log; upstream videohint { # server 167.172.58.156:443; server localhost:3000; } server { listen 443 ssl; server_name localhost; ssl_certificate /etc/ssl/localhost/localhost.crt; ssl_certificate_key /etc/ssl/localhost/localhost.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_session_timeout 1d; ssl_stapling off; ssl_stapling_verify off; add_header Strict-Transport-Security max-age=15768000; add_header X-Frame-Options ""; proxy_ssl_name "www.videohint.com"; proxy_ssl_server_name on; location ~ /socialLoginSuccess { return 301 https://$host:8000/#/socialLoginSuccess; } location ~ /auth/(.*) { proxy_pass https://videohint/key/auth/$1?$query_string; proxy_set_header Host localhost; } location / { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Accept-Encoding ""; proxy_set_header Proxy ""; proxy_pass https://localhost:8000/; # proxy_pass https://www.bing.com/ # These three lines added as per https://github.com/socketio/socket.io/issues/1942 to remove socketio error proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } } include servers/*; }
- 核心需求:将
https://localhost:8000/stripe_checkout/...路径的请求重定向至https://checkout.stripe.com/...,其余请求保留原有逻辑不变
解决方案
只需在Nginx配置的server块中,所有现有location规则的最前面添加重定向规则,利用Nginx从上到下的匹配优先级优先处理该路径请求:
# 新增:处理stripe_checkout路径的重定向 location ^~ /stripe_checkout/ { return 301 https://checkout.stripe.com$request_uri; }
修改后的完整Nginx配置
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format my_log '{ "time": "$time_iso8601", ' '"remote_addr": "$remote_addr", ' '"status": "$status", ' '"request": "$request", ' '"request_method": "$request_method", ' '"http_referrer": "$http_referer", ' '"http_x_forwarded_for": "$http_x_forwarded_for", ' '"host": "$host", ' '"server_name": "$server_name", ' '"upstream_address": "$upstream_addr", ' '"upstream_status": "$upstream_status" }'; access_log /usr/local/var/log/nginx/access.log; upstream videohint { # server 167.172.58.156:443; server localhost:3000; } server { listen 443 ssl; server_name localhost; ssl_certificate /etc/ssl/localhost/localhost.crt; ssl_certificate_key /etc/ssl/localhost/localhost.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_session_timeout 1d; ssl_stapling off; ssl_stapling_verify off; add_header Strict-Transport-Security max-age=15768000; add_header X-Frame-Options ""; proxy_ssl_name "www.videohint.com"; proxy_ssl_server_name on; # 新增:处理stripe_checkout路径的重定向 location ^~ /stripe_checkout/ { return 301 https://checkout.stripe.com$request_uri; } location ~ /socialLoginSuccess { return 301 https://$host:8000/#/socialLoginSuccess; } location ~ /auth/(.*) { proxy_pass https://videohint/key/auth/$1?$query_string; proxy_set_header Host localhost; } location / { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Accept-Encoding ""; proxy_set_header Proxy ""; proxy_pass https://localhost:8000/; # proxy_pass https://www.bing.com/ # These three lines added as per https://github.com/socketio/socket.io/issues/1942 to remove socketio error proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } } include servers/*; }
配置说明
^~ /stripe_checkout/:使用前缀匹配并标记为优先匹配,确保Nginx先处理该路径请求,不会被后续正则location规则覆盖return 301:永久重定向,若需临时重定向可改为302$request_uri:保留原请求的完整路径和查询参数,确保重定向后的地址包含原请求的所有附加信息
内容的提问来源于stack exchange,提问作者SoftTimur
相关产品推荐
相关产品推荐

