Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX proxy_pass same protocol (http/https)

Tags:

nginx

I have a section in my NGINX config file that uses proxy_pass to redirect API traffic to upstream servers. I have the locations in one server block that serves both http and https requests:

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name mysite.local;    # Valid TLD in production

I then have location blocks to define my API gateways:

    location /api/v1/json {
        # Various proxy config directives
        proxy_pass http://upstream;

My question is: would it be possible to drop http:// and pass requests to my upstream servers depending on the protocol without splitting my server block? Something like HTML/JavaScript //mysite.local requests.

like image 653
RalphORama Avatar asked Oct 01 '17 19:10

RalphORama


People also ask

What does proxy_pass do in nginx?

The proxy_pass setting makes the Nginx reverse proxy setup work. The proxy_pass is configured in the location section of any virtual host configuration file. To set up an Nginx proxy_pass globally, edit the default file in Nginx's sites-available folder.

What protocol does Nginx use?

The PROXY protocol enables NGINX and NGINX Plus to receive client connection information passed through proxy servers and load balancers such as HAproxy and Amazon Elastic Load Balancer (ELB). With the PROXY protocol, NGINX can learn the originating IP address from HTTP, SSL, HTTP/2, SPDY, WebSocket, and TCP.

Why use nginx reverse proxy?

Security and anonymity – By intercepting requests headed for your backend servers, a reverse proxy server protects their identities and acts as an additional defense against security attacks.

Can Nginx be used as forward proxy?

NGINX was initially designed as a reverse proxy server. However, with continuous development, NGINX also serves as one of the options to implement the forward proxy. The forward proxy itself is not complex, the key issue it addresses is how to encrypt HTTPS traffic.


1 Answers

You could use the $scheme variable:

location /api/v1/json {
    # Various proxy config directives
    proxy_pass $scheme://your-host
}

From the docs:

$scheme
request scheme, "http" or "https"

Which would then use the same protocol as the original request.

like image 113
nbari Avatar answered Oct 09 '22 11:10

nbari