Checking proxy set header forwaded by nginx revers

2019-09-11 09:04发布

问题:

I'm using nginx as reverse proxy with gunicorn for my Django app, and am new to webserver configuration. My app has a postgres backend, and the machine hosting it has Ubuntu 14.04 lts installed.

I have reason to suspect that my nginx configuration is not forwarding proxy set header to the Django app correctly. Is there a way I can see (e.g. print?) the host, http_user_agent, remote_addr etc. forwarded, on the linux command line to test my gut feel?

Secondly, how do I check whether my Django app received the correct forwarded IP? E.g. can I somehow print?

/etc/nginx/sites-available/myproject:

server {
    listen 80;
    server_name example.cloudapp.net;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/mhb11/folder/myproject;
    }

    location / {
        proxy_set_header Host $host;
        proxy_set_header User-Agent $http_user_agent;
        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 $scheme;
        proxy_pass http://unix:/home/mhb11/folder/myproject/myproject.sock;
    }
   error_page 500 502 503 504 /500.html;
   location = /500.html {
        root /home/mhb11/folder/myproject/templates/;
   }
}

回答1:

All you have to do is print out request.META at the Django project level to see what all of those values are being set to. This automatically happens for you if you get an error and debug is set to True (just scroll down, you'll see a big table with all request.META values populated therein).

Or you can print it yourself from your views.py, or if that doesn't work, then from any middleware you have. You can even write custom middleware for this. Let me know if you need further clarification on this, I can give you basic code snippets too.