Set Custom Header in Nginx config and pass it to gunicorn

梦想的初衷 提交于 2020-06-27 17:08:01

问题


I am trying to set a custom header in nginx and finally passing it to gunicorn server which runs the django application. After changing the nginx config file, I checked the request.META dictionary in django, but my custom header is missing. I believe nginx is not sending the header properly. This is my relevant nginx config file content.

server {
   listen 443;
   server_name www.example.com;
   client_max_body_size 40M;
   ssl on;
   ssl_certificate /home/ubuntu/prodStuff/ssl/server.crt;
   ssl_certificate_key /home/ubuntu/prodStuff/ssl/server.key;

   ssl_session_timeout 5m;
   ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
   ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
   ssl_prefer_server_ciphers on;

    access_log /home/ubuntu/logs/nginx-access.log;
    error_log /home/ubuntu/logs/nginx-error.log info;
    set $mobile_rewrite perform;
     location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            if ($mobile_rewrite = perform) {
              proxy_set_header renderMobileWebsite "1";
            }
            else{
              proxy_set_header renderMobileWebsite "0";
            }
            proxy_pass_header renderMobileWebsite;
            if (!-f $request_filename) {
            proxy_pass http://djangoapp_server;
            break;
            }
    }

Then I am just printing request.META but there is no entry of renderMobileWebsite header. This is the output of printing request.META

{'HTTP_COOKIE': '_gat=1; csrftoken=mGKNffeg7DjwvAsulLigzYwzni5eu; 
_ga=GA1.2.1693535173.1467728753; 
sessionid=ye46dsu9ff68jktbdfhdrhhdielu2np2e0g; 
wcsid=61ln3H0NVEu1RDhf285Ly32sDJ04QKE2; 
hblid=wVO8zgtwCPYjhGjb285Ly32sDJ4EQK0a; 
_oklv=1457358771468%2C61ln3H0NVEu1RDhf285Ly32sDJ04QKE2; 
_ceg.s=o3o983; _ceg.u=o3o983', 'SERVER_SOFTWARE': 'gunicorn/19.2.1', 
'SCRIPT_NAME': u'', 'REQUEST_METHOD': 'POST', 'PATH_INFO': u'/product/updateSwatch/', 
'HTTP_ORIGIN': 'https://www.example.com', 
'SERVER_PROTOCOL': 'HTTP/1.0', 
'QUERY_STRING': '', 
'CONTENT_LENGTH': '107', 
'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36', 
'HTTP_CONNECTION': 'close', 'HTTP_REFERER': 'https://www.example.com/catalogue/hazel/', 
'SERVER_NAME': '127.0.0.1', 
'REMOTE_ADDR': '127.0.0.1', 
'wsgi.url_scheme': 'https', 
'SERVER_PORT': '8000', 
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest', 
'HTTP_X_FORWARDED_PROTO': 'https', 
'REMOTE_PORT': '45389', 
'wsgi.input': <gunicorn.http.body.Body object at 0x7fc0117d87d0>, 
'HTTP_HOST': 'www.example.com', 
'wsgi.multithread': False, 
'HTTP_ACCEPT': '*/*', 'wsgi.version': (1, 0), 
'RAW_URI': '/product/updateSwatch/', 
'wsgi.run_once': False, 'wsgi.errors': <gunicorn.http.wsgi.WSGIErrorsWrapper object at 0x7fc0117d8450>, 
'wsgi.multiprocess': True, 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8', 
'gunicorn.socket': <socket._socketobject object at 0x7fc011855e50>, 
'HTTP_X_FORWARDED_PORT': '443', 
'CONTENT_TYPE': 'application/x-www-form-urlencoded; charset=UTF-8', 
'HTTP_X_FORWARDED_FOR': '115.118.144.31, 172.31.17.146', 
'wsgi.file_wrapper': <class 'gunicorn.http.wsgi.FileWrapper'>, 
'HTTP_ACCEPT_ENCODING': 'gzip, deflate'}

Can anyone suggest me what am I doing wrong here.


回答1:


The primary problem is that proxy_set_header directives are not allowed in the if block.

However, the same functionality can be accomplished using a map directive. Using the following within the http block:

map $mobile_rewrite $render_mobile_rewrite {
    default     0;
    perform     1;
}

And the following in your existing location block:

proxy_set_header renderMobileWebsite $render_mobile_rewrite;

See this document for details.



来源:https://stackoverflow.com/questions/35845155/set-custom-header-in-nginx-config-and-pass-it-to-gunicorn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!