So I am trying to setup proxy pass to a rest api that is on port 30422, basically what I want to do is have for example sub-domain.example.com link to the port 30422 on the same webhost. I am hosting the webserver on nginx, and have linked a domain already.
What I have tried was adding my new sub-domain on cloudflare (I use cloudflare as protection) And then also making my new subdomain file in the sites-available folder and symlinking it to sites-enabled folder.
This was my first attempt, which didn't work:
server {
listen 80; # Port on which Nginx will listen for incoming requests
server_name sub-domain.example.com; # Your domain name or server IP
location / {
proxy_pass http://example.com:30422; # Address of your REST API server
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Then I tried to use curl on the vps itself on the subdomain, that didn't work. Then I tried to ping the actual vps ip with the port 30422 and that returned the wanted response from the rest api, that is hosted on the same vps.
So instead I updated my nginx config to instead go thru the vps ip and the port as proxy pass:
server {
listen 80; # Port on which Nginx will listen for incoming requests
server_name sub-domain.example.com; # Your domain name or server IP
location / {
proxy_pass http://x.x.x.x:30422; # Address of your REST API server
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Which in turn didn't work either.
I also tried this configuration, but to no avail.
server {
listen 80;
server_name sub-domain.example.com;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:30422;
proxy_redirect off;
}
}
This is my first attempt at doing this, and I am just trying to learn. I have looked for answers but none of the solutions I tried worked.
Edit: I even tried disabling always https for cloudflare, and then going back to using proxy_set_header. Still doesn't work.
This is the output I get when using curl with verbose output (sub-domain.example.com)
* Trying 104.21.93.82:80...
* TCP_NODELAY set
* Connected to sub-domain.example.com (104.21.93.82) port 80 (#0)
> GET / HTTP/1.1
> Host: sub-domain.example.com
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Mon, 12 Jun 2023 20:40:35 GMT
< Content-Type: application/octet-stream
< Content-Length: 0
< Connection: keep-alive
< CF-Cache-Status: DYNAMIC
< Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=bO266eNsXPsKOXRa3nqoNi3wRJPkkYH8a35%2Bvcaq%2B43iQtBIqIk0Lgbf0R8%2BOpCtE6Xc1jYWskuZo6f4XWKa9GRpcCzP5E9NdC6F6kSs9HFLca2uuctXESwAdq%2BO%2FWT9t1EAyuB%2FhRmdGu%2BdWA%3D%3D"}],"group":"cf-nel","max_age":604800}
< NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
< Server: cloudflare
< CF-RAY: 7d64e8e85b75414c-LHR
< alt-svc: h3=":443"; ma=86400
<
* Connection #0 to host sub-domain.example.com left intact