Redirect url HTTP post to HTTPS in nginx

3

I have an application that returns POSTS from the payment system, but we currently force SSL to our site, I made the HTTP redirection 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 does not try) the 404 error appears.

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 any possibilities to make this happen?

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

    
asked by anonymous 14.10.2016 / 01:26

2 answers

1

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

Following João'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, holding the headers. To test we can create a nodejs server by listening to 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 server node just use the commands:

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

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

When performing the whole process we will have as input to this file:

{"product": "New product", "id": "1"}
    
19.10.2016 / 06:29
0

In fact, according to HTTP spec , user-agent 302 should not redirect the entire request. I believe that what happens is that your request, when it reaches 302, becomes a GET , and this causes your 404 (since the server is probably only waiting for POST ).

I think ideally you should use a Nginx reverse-proxy function called Proxy Pass .

Update your redirection to:

if ($scheme = http) {
  proxy_pass_header on;
  proxy_pass https://$server_name$request_uri;
}

can solve your problem. The first line activates the sending of the headers, which will be necessary for your POSTback and are disabled by default, and the second one defines the pro proxy url.

    
16.10.2016 / 04:13