ASP.NET Core+Nginx环境下Ajax请求异常:OPTIONS替代GET,连接拒绝
Hey Diego, sorry to hear you hit this snag after deploying your ASP.NET Core app from Windows to Ubuntu—even with no CORS violations, there are a handful of common gotchas with this setup. Let’s walk through the most likely fixes:
1. Verify Kestrel is actually running and responding
First, rule out that your app itself isn’t the problem. On your Ubuntu server:
- Check if the ASP.NET Core service is active (if you’re using systemd to manage it):
systemctl status your-app-name.service - If it’s running, test a direct request to Kestrel’s port (5000) to confirm it serves content:
curl http://localhost:5000/your-test-endpoint - If the curl fails, check the app logs for errors with:
(Thejournalctl -u your-app-name.service -f-fflag lets you watch logs in real-time as you make requests.)
Also, double-check that Kestrel is binding to all network interfaces (not just localhost). In your Program.cs, make sure you have something like:
builder.WebHost.ConfigureKestrel(options => { options.ListenAnyIP(5000); // Binds to 0.0.0.0:5000 });
Or set the Urls property in appsettings.json:
"Urls": "http://0.0.0.0:5000"
2. Audit your Nginx reverse proxy configuration
Nginx misconfigurations are the #1 culprit here. Open your site’s Nginx config (usually in /etc/nginx/sites-available/your-site.conf) and confirm it includes these critical settings:
server { listen 8080; server_name your-server-ip-or-domain; location / { proxy_pass http://localhost:5000; # Required for ASP.NET Core to handle requests properly proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; # Pass the original host header so the app knows where requests are coming from proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; # Forward client IP and protocol info to the app proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
After making changes:
- Validate the config syntax with:
nginx -t - Restart Nginx to apply changes:
systemctl restart nginx
3. Check Ubuntu firewall settings
If you’re using UFW (Ubuntu’s default firewall), make sure port 8080 is open for incoming traffic:
- Check current rules:
ufw status - If 8080 isn’t listed, add it:
ufw allow 8080/tcp ufw reload
4. Inspect Chrome’s detailed error info
Since you mentioned Chrome throws an error, dig into the Network tab in DevTools:
- Look at the failed request’s status code (e.g., 502 Bad Gateway, 404 Not Found)
- Check the "Response" and "Headers" tabs for more context
- If it’s a 502, that almost always means Nginx can’t reach Kestrel (go back to step 1)
- If it’s a 404, confirm your app’s routing matches the request path, or that static files are being served correctly
If you can share the exact error message or status code from Chrome, we can narrow this down even further.
内容的提问来源于stack exchange,提问作者Diego Alves

