我一直在本地 Linux 服务器上进行开发,使用 Stripe CLI 让 Cashier 处理端点(这意味着我没有为 /stripe/webhook 设置自定义路由)和 Webhooks,它工作得很好。在我的 NGINX 服务器上,我在将 Webhook 发送到我的服务器时遇到问题(例如 https://api-xxx.xxx.com/stripe/webhook)。 // 使用 xxx 隐藏我的实际 URL。
我遇到过 405、404 和 403 错误。在 Stripes 端,它正在调用正确的位置。在我的服务器日志中,我可以看到这些尝试。
[25/Sep/2024:15:28:45 +0000] "POST /stripe/webhook HTTP/1.1" 404 146 "-" "Stripe/1.0 (+https://stripe.com/docs/webhooks)"
我已经尝试过了,但没有任何效果:https://distinctplace.com/2017/04/17/405-not-allowed-nginx-fix-post-requests/
服务器:NGINX | 前端:反应 | 后端: Laravel | 构建工具:Vite
server {
listen 80;
listen 443 ssl http2;
server_name api-xxx.xxx.com;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/api-xxx.xxx.com/public;
# SSL Configuration
...
# Error pages
error_page 404 /404.html;
error_page 497 https://$host$request_uri;
error_page 502 /502.html;
# PHP handling
location / {
proxy_pass https://api-xxx.xxx.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
try_files $uri $uri/ /index.php?$query_string;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/tmp/php-cgi-83.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /stripe/webhook {
proxy_pass https://api-xxx.xxx.com/stripe-webhook;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
# Static files expiration
location ~* \.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
error_log /dev/null;
access_log /dev/null;
}
location ~* \.(js|css)$ {
expires 12h;
error_log /dev/null;
access_log /dev/null;
}
# MIME type configuration for WebP files
types {
image/webp webp;
}
location = /favicon.ico {
log_not_found off; # Suppress error messages for missing favicon
access_log off; # Avoid unnecessary logging for favicon requests
}
# Access and error logs
access_log /www/wwwlogs/api-xxx.xxx.com.log;
error_log /www/wwwlogs/api-xxx.xxx.com.error.log;
# Well-known directory for SSL certificate verification
location ^~ /.well-known {
allow all;
}
# Deny access to sensitive files in the well-known directory
location ~* ^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$ {
return 403;
}
# Forbidden files or directories
location ~* ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md) {
return 404;
}
# Location for serving files from storage directory
location /storage {
alias /www/wwwroot/api-xxx.xxx.com/storage/app/public;
try_files $uri $uri/ /index.php?$query_string;
}
}
我尝试过的资源: https://distinctplace.com/2017/04/17/405-not-allowed-nginx-fix-post-requests/
Webhook URI 应类似于
/stripe/webhook
而不是 /stripe-webhook
。
如果是这样,您必须像这样更新您的 Nginx 配置(仅是块)。
location /stripe/webhook {
proxy_pass https://api-xxx.xxx.com/stripe/webhook;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}