Redirect HTTP post url to HTTPS in nginx

Question:

I have an application that I receive POSTS return from the payment system, but currently we force the use of SSL for our website, I redirected HTTP to HTTPS, but I get a 404 error when the POSTBACK system sends the POST to the URL http:// …., when my server tries to redirect to HTTPS (Or not) I get the 404 error.

NGNIX file:

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/magicaonline.com.br/before/*;

server {
    listen 80;
    listen [::]:80;
    return 307 https://magicaonline.com.br/$1;
}

server {
    listen 443 http2;
    listen [::]:443 http2;
    server_name .magicaonline.com.br;
    root /home/forge/magicaonline.com.br/public;

    if ($host = 'www.magicaonline.com.br') {
        rewrite ^/(.*)$ https://magicaonline.com.br/$1 permanent;
    }

    #if ($scheme = http) {
    #    return 302 https://$server_name$request_uri;
    #}

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/magicaonline.com.br/132754/server.crt;
    ssl_certificate_key /etc/nginx/ssl/magicaonline.com.br/132754/server.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'xxxxx';
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DOT NOT REMOVE!)
    include forge-conf/magicaonline.com.br/server/*;

    location /lp/ {
        try_files $uri $uri/ @wordpress;
    }

    location @wordpress {
        rewrite /lp/ /lp/index.php;
    }

    location ^/lp/index.php(/.*)?$ {
        fastcgi_split_path_info ^(/lp/index.php)(/.*)$;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO $fastcgi_path_info;
        include fastcgi_params;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/magicaonline.com.br-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/magicaonline.com.br/after/*;

Are there possibilities to make this happen?

I need this solution because the payment system does not accept modifying the POSTBACK URL.

Answer:

The 302 turns your POST into a GET, the browser itself performs this behavior.

Following Joao's recommendation, which is correct, we have a simple case:

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format compression '$remote_addr - $remote_user [$time_local] '
                           '"$request" - "$request_body" - $status $body_bytes_sent '
                           '"$http_referer" "$http_user_agent" "$gzip_ratio"';

    server {
        listen       8181;
        server_name  localhost;

        access_log /my_path/log/nginx.log;

        location / {
            proxy_pass_header on;
            proxy_pass http://localhost:8080/receive;
        }
    }

    include servers/*;
}

Here we have a server listening on port 8181, with log writing enabled. When performing a simple post using curl in this route:

curl -X POST -H 'Content-Type: application/json' -d '{"product": "New product", "id": "1"}' http://localhost:81

The request will be redirected to port 8080, keeping the headers. To test we can create a nodejs server listening on this port:

var express = require('express')
var fs = require('fs')
var url = require('url')
var app = express()

app.post('/receive', function(request, respond) {
    var body = ''

    filePath = __dirname + '/post.txt'
    request.on('data', function(data) {
        body += data
    })

    request.on('end', function (){
        body = body + '\n'
        fs.appendFile(filePath, body, function() {
            respond.end()
        })
    })
})

app.listen(8080)   

To run the node server just use the commands:

npm init
npm install --save express
node index.js

This server receives the redirect from nginx and writes the post to a file called post.txt.

When carrying out the entire process, we will have as input in this file:

{"product": "New product", "id": "1"}
Scroll to Top
AllEscort