Django 1.11基于passenger_wsgi部署时POST请求无法路由问题求助
Hey there, let's work through your POST routing problem with Passenger on A2 Hosting. I see your app runs perfectly with runserver (both locally and over SSH tunnel), but once deployed via passenger_wsgi, POST requests aren't being handled right. Let's break down what might be going on and how to fix it.
First, here's the partial passenger_wsgi.py code you shared, formatted for clarity:
import os import sys sys.path.insert(0, "/home/<username>/app") import APP_CORE # where is the python interpreter INTERP = "/home/<username>/app/.virtualenv/bin/python" if sys.executable ...
Common Fixes to Try
1. Explicitly Define Your WSGI Application Object
Passenger relies on a named application variable to serve requests—right now, you're importing APP_CORE but not exposing that critical object.
If you're using Flask, add this line at the end of your passenger_wsgi.py:
from APP_CORE import app as application # Swap "app" with your actual Flask app name if different
For Django, you'll need to point to your project's built-in WSGI object:
# Add this after sys.path.insert sys.path.insert(0, "/home/<username>/app/your_project_name") # Replace with your project folder from your_project_name.wsgi import application
2. Finish the Virtual Environment Activation Block
Your INTERP line points to your virtualenv's Python, but you need to complete that conditional to ensure Passenger uses it instead of the system Python. Add this:
if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
Without this, Passenger might be using a mismatched Python environment, leading to missing dependencies or config inconsistencies that break POST routing.
3. Check for App-Specific Restrictions
Dev servers are often more forgiving than production WSGI setups. Double-check:
- CSRF protection: If you're using Django or Flask-WTF, make sure CSRF tokens are included in POST requests, and production allowed origins are configured correctly.
- Middleware rules: Are there any middleware blocks that restrict POST requests unless certain headers are present?
- Route patterns: Are your POST routes case-sensitive or have typos that the dev server ignored but Passenger doesn't?
4. Verify Your .htaccess Configuration
A2 requires a .htaccess file to tell Passenger how to route requests. Make sure yours looks like this (adjust paths to match your setup):
PassengerPython /home/<username>/app/.virtualenv/bin/python WSGIScriptAlias / /home/<username>/app/passenger_wsgi.py <Directory /home/<username>/app> AllowOverride All Options -MultiViews Require all granted </Directory>
If WSGIScriptAlias isn't set to route all requests (/), POST requests to specific paths might never reach your app.
5. Dig Into Error Logs
A2 keeps Passenger error logs that will tell you exactly what's breaking. You can access them via SSH or your hosting control panel, usually at:
/home/<username>/logs/domains/yourdomain.com/passenger.log
Look for tracebacks related to POST requests—404 errors, permission issues, or exceptions in your view code will give you direct clues.
Next Steps to Test
- Complete the
passenger_wsgi.pycode with the WSGI application line and virtualenv activation block. - Double-check your
.htaccessfile for routing rules. - Check the Passenger error logs for specific error messages.
- Test a minimal POST endpoint (e.g., a simple view that returns
{"status": "ok"}) to see if the issue is with routing or your specific view logic.
内容的提问来源于stack exchange,提问作者Zen

