How set dynamic proxy_pass in nginx like gitpod.com:
I already have a wildcard certificate
For example, in Gitpod you have a VM and if you start a port like 8081, your URL is:
https://8081-some-uuid.ws-us02.gitpod.io/
Following this order of ideas, I would like to configure something like
8082.example.com -> http://localhost:8082
8081.example.com -> http://localhost:8081
8080.example.com -> http://localhost:8080
site-enabled/example-com.config
server {
server_name *.example.com;
listen 80;
location / {
// how config this??
proxy_pass http://localhost:(¿dynamic port?);
proxy_set_header Connection 'upgrade';
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
}
}
Instead of using a wildcard name with server_name, you could use a regular expression to capture the subdomain part of the request. See this document for details.
For example:
server_name "~^(?<subdomain>[0-9]{4})\.example\.com$";
proxy_pass http://localhost:$subdomain;
Related
I was trying to route my localhost:8585 requests to some external url, say https://someExternalSite.xyz. Such that i can access the external site using localhost port url in my app.
i.e if i curl localhost:8585, it should actually return the output of curl https://someExternalSite.xyz
I have tried this configuration but not working
server {
listen 8585;
listen [::]:8585;
server_name localhost;
location / {
proxy_pass https://someExternalSite.xyz/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
}
}
Need some help
I am installing grandnode on ubuntu server. I installed nginx and configured it as a reverse proxy. In the sites-available directory, I created the reverse-proxy.conf file and wrote the following code in it:
server {
listen 80;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
And i was able to access the site using my ip address. But now, how do I access the site by domain name? For example: example.com
Did you create a custom conf file in /etc/nginx/conf.d/ directory?
Working file /etc/nginx/conf.d/reverse-proxy.conf
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Make sure after the changing nginx configuration reload or restart.
i have three docker containers on different ports and would like to implement reverse proxy with nginx to each of the containers so that i can navigate to each of the containers by passing a keyword instead of ports. Like Instead of http://localhost:3000 i want to pass like http://localhost/app1
I created the nginx image with below dockerfile.
FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY index.html usr/share/nginx/html
and my nginx conf file looks like this:
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
upstream app1 {
server localhost:3000;
}
upstream app2 {
server localhost:3001;
}
server {
listen 3000;
location /app1 {
proxy_pass http://localhost:3000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
server {
listen 3001;
location /app2 {
proxy_pass http://localhost:3001;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
but after creating the nginx container from the image. I see the container created. but when i tried to access with localhost it timed out so nginx is not running or did i miss something. I cannot load the container with localhost/app1 or app2 or even localhost is not working. help needed. thanks in advance.
You are exposing the ports 80 and 443 when you start the container but I don't see you listening to those ports in the nginx configuration.
Please try replacing listen 3000 by listen 80 and then try accessing localhost/app1
I can also see that you are using --link when you start your docker container. So I think you should use app1 and app2 instead of localhost. Please let me know if there is something that I missed so it isn't the case. You must also make sure that your applications are accessible on these ports (3000 and 3001).
Also your 2 locations should be in the same server block:
server {
listen 80;
location /app1 {
proxy_pass http://app1:3000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
location /app2 {
proxy_pass http://app2:3001;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
I have a simple HTTP server written in Go.
In development It works fine but for production, where this server has to handle 100 requests at a time I need a proper web server like nginx.
How can I put it behind nginx?
I'm guessing you need a simple reverse proxy config.
Lets say your go http server is listening on http://example.com:8080 :
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://example.com: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;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
How to proxy all subdomain to other domain path?
For example
SUBDOMAIN.abcxyz123.com
To be proxied to
myapp.otherdomain.com/SUBDOMAIN
Making sure that all header/path and query parameters in the request is kept.
Update:
I've tried and have a working config but still not the one I need:
server {
listen 80;
server_name ~^(?<subdomain>.+)\.abcxyz123\.com$;
location / {
proxy_set_header Host "myapp.otherdomain.com";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
# this worked:
proxy_pass http://myapp.otherdomain.com/somepath/;
# this does not work:
#proxy_pass http://myapp.otherdomain.com/$subdomain$request_uri;
}
}
Try this
server {
server_name ~^(?<subdomain>.*)\.abcxyz123\.com$;
resolver 8.8.8.8;
rewrite ^/(.*)$ /$subdomain/$1;
location / {
proxy_set_header Host "myapp.otherdomain.com";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_pass http://myapp.otherdomain.com;
}
}
This should proxy all your traffic with original query parameters(query strings, request body, request method, etc), I changed the host header to the proxied "myapp.otherdomain.com" incase the server of 'myapp.otherdomain.com' has more than one virtual hosts. If you don't want the change, use $host instead.
This answer might need another edit since your question isn't very clear. If you have further question, comment and i will edit in my answer.