Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect http post request to https post request via proxy in nginx

Tags:

nginx

proxy

https

I want to redirect a http post request to https post request.Is there a way to complete it through the proxy configuration in nginx. I find a blog in the http://nginx.com/blog/nginx-ssl/. And I have a try like this:

upstream backends {
   server 192.168.100.100:443;
}

server {
   listen              80;
   server_name         www.example.com;
   location / {
       proxy_pass https://backends; 
   }
}

But it returned a 502 bad gateway result when I curl it like this: curl -X POST 'http://www.example.com/a.json' --data-binary 'name=super'

like image 662
LoVeSong Avatar asked Jul 23 '26 05:07

LoVeSong


1 Answers

You need to use the 308 redirect instead of 301 (former keeps the method and latter mutates POSTs to GETs):

server {
  listen 80;
  server_name www.example.com;
  return 308 https://$host$request_uri;
}
like image 82
pathikrit Avatar answered Jul 25 '26 18:07

pathikrit