skip to Main Content

I am testing some APIs with POSTMAN.

When i am sending data in request body (raw section in POSTMAN) , data is present when i call the url with https i.e https://example.com/api-url/ but i am receiving empty body when i send the request with http url i.e http://example.com/api-url/

Non-secure requests are working fine. They are being directed to https. Only issue is request body is not being there when any request is called from http url.

What is wrong in nginx configuration?

This is the nginx configuration.

server {
    server_name  www.example.com;
    return       301 $scheme://example.com$request_uri;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {

        root /usr/share/nginx/html;
        index index.html index.htm;

        client_max_body_size 4G;

        access_log /home/myuser/myproject/logs/nginx-access.log;
        error_log /home/myuser/myproject/logs/nginx-error.log;

        server_name example.com ;
        
        add_header Content-Security-Policy "frame-ancestors *.exampledomain.com" always;

        location /static/ {  alias   /home/myuser/myproject/staticfiles/; }    
        location /media/ {  alias   /home/myuser/myproject/media/; }

        

        location / {
                     proxy_pass http://unix:/home/myuser/myvenv/myproject/daphne.sock;
                     proxy_http_version 1.1;
                     proxy_set_header Upgrade $http_upgrade;
                     proxy_set_header Connection "upgrade";
                     proxy_redirect     off;
                     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-Host $server_name;
                     proxy_read_timeout 240;
                     proxy_connect_timeout 240;
                     proxy_send_timeout 240;
                     send_timeout 240;
        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name example.com ;
    return 404; # managed by Certbot
}

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name  www.example.com;
    return 404; # managed by Certbot
}

2

Answers


  1. Chosen as BEST ANSWER

    I found this answer by @miknik . It states that there is an issue while redirecting with 301/302 status codes. I replaced 301 with 307 in my nginx configuration and it started working.


  2. Maybe Express body-parser/json-parser didn’t parse the body when you call with http, and did not pass it to req.body.
    Add this into your / proxy handler May correct your problem :

                     proxy_set_header content-type "application/json";
    

    like this :

        location / {
                     proxy_pass http://unix:/home/myuser/myvenv/myproject/daphne.sock;
                     proxy_http_version 1.1;
                     proxy_set_header Upgrade $http_upgrade;
                     proxy_set_header Connection "upgrade";
                     proxy_set_header content-type "application/json";
                     proxy_redirect     off;
                     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-Host $server_name;
                     proxy_read_timeout 240;
                     proxy_connect_timeout 240;
                     proxy_send_timeout 240;
                     send_timeout 240;
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search