NGINX rewrite http to https

175 Views Asked by At

I am trying to rewrite HTTP to HTTPS. It is working properly in browser it is redirecting even if I access through private IP. But when I send request from another server it is returning HTTP/1.1 301 Moved Permanently.

My scenario:

  • Client sends request to public IP (X.X.X.X:80) - F5 proxy
  • X.X.X.X:80 proxy to my server Y.Y.Y.Y:80 (NGINX)
  • On my server NGINX rewrites all http request to https.
  • Finally, NGINX proxy pass to NodeJS and Java services.

Here is my NGINX config.

server {
        listen          80;
        server_name     private-domain.company.local;
        return  301     https://$server_name$request_uri;
}
server {
        listen  443     ssl;
        server_name     private-domain.company.local;
        access_log      /var/log/nginx/web.access.log;
        error_log       /var/log/nginx/web.error.log;

        ssl_certificate         /etc/nginx/ssl/web.crt;
        ssl_certificate_key     /etc/nginx/ssl/web.key;
        ssl_ciphers             EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
        ssl_protocols           TLSv1.1 TLSv1.2;

        location /{
                proxy_pass      http://localhost:3000;
        }

        location /api {
                proxy_pass      http://localhost:8050/api/;
        }

}

When I try to curl from another server, it is returning 301.

curl -X POST http://X.X.X.X/api/url/to/send -v

The response:

*   Trying X.X.X.X...
* TCP_NODELAY set
* Connected to X.X.X.X (X.X.X.X) port 80 (#0)
> POST /api/url/to/send HTTP/1.1
> Host: X.X.X.X
> User-Agent: curl/7.61.1
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.14.1
< Date: Mon, 03 Apr 2023 07:42:45 GMT
< Content-Type: text/html
< Content-Length: 185
< Connection: keep-alive
< Location: https://private-domain.company.local/api/url/to/send
<
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.14.1</center>
</body>
</html>
* Connection #0 to host X.X.X.X left intact

I have tried few solutions on internet, but still no luck for me.

0

There are 0 best solutions below