I am kinda new to Nginx. I am trying to create a reverse_proxy server. I have XAMPP running on localhost:8080 and I run Nginx on example.test:80 I don't want the URL to change on the client-side (in the browser) but no matter what I try, it keeps changing it. I somehow managed to keep the host as example.test but still the port changes to 8080 (which doesn't make any sense as I can access the XAMPP with example.test:8080 without nginx)
events {}
http {
server {
listen 80;
server_name example.test;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Preserve original request URI and scheme
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-URI $request_uri;
# Mask the URL on the client side
proxy_buffering off;
client_max_body_size 0;
proxy_read_timeout 36000s;
proxy_http_version 1.1;
proxy_request_buffering off;
proxy_connect_timeout 5s;
# Rewrite rule to remove the port number from the URL
rewrite ^/(.*)$ $scheme://$host/$1 break;
# Add the following line to preserve the server_name in the client's URL
rewrite ^/(.*)$ /$1 break;
}
}
}
Also, if I access example.test
without mentioning any path, it works perfectly. But as soon as I add a path like example.test/new
, the URL in the browser changes to either localhost:8080/new
or example.test:8080/new
.
I just need localhost:8080
to be example.test
in the browser. Can someone tell me what am I doing wrong here?