Forward a port with NGINX - nginx

I am trying to forward an python application to a directory. I know I worded this badly, let me try and explain it better
What I currently have:
NGINX on port 80
Static HTML files on /var/www/html
Python API on port 5500
localhost:5500/apiEndpoint (for example)
How can I change it so if I go to
localhost:80/api/apiEndpoint
it gives me localhost:5500/apiEndpoint
?
Thank you for your help :)

If you want to do a mask of that location, you can try:
server {
listen 80;
listen [::]:80;
root /var/www/html;
server_name domain.com;
location ~ ^/api/apiEndpoint/(.*)$ {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:5500/apiEndpoint/$1;
}
}
Hope i understood your question correctly.

Related

Nginx 1.18 (on Ubuntu 20.04) proxy_pass not working

I'm new to Nginx proxy_pass, I want to convert app1 subdomain to path of app1. and url should be subdomain. I tried again and again but no luck. I'm stuck in this matter for two days.
server {
listen 80;
server_name app1.example.com;
location / {
proxy_pass http://example.com/app1$request_uri;
proxy_set_header Host app1.example.com;
}
}
Output : is 502 Bad gate way.
But when I access manually the http://example.com/app1 , it showing fine. Can't accessing from proxy_pass.
I tried many ways from the blog internet but not solved.
Thanks in advance.

Dynamically proxy to subdomain according to path with nginx?

I am attempting to convert all traffic to its "matching" internal equivalent, and proxy the traffic via this new internal URL.
For example,
http://external.com/image/blue.png will map to http://image.internal.com/blue.png
I have followed the guide here to attempt to set this up via nginx, however I am receiving the following error:
nginx: [emerg] unknown "path" variable
Below is my nginx configuration file:
server {
listen 80;
listen [::]:80;
root /var/www/private/$subdomain;
index index.html index.htm index.nginx-debian.html;
server_name external.com;
location ~ ^/(?<subdomain>[^/]+)/(<path>.*)?$ {
proxy_pass http://$subdomain.internal.com/$path;
proxy_buffering off;
proxy_set_header Host $http_host;
}
}
Do I need to define the 'path' variable somewhere else? All help is appreciated, thank you.
You're missing a question mark. It's ?<path> not <path>

nginx Reverse Proxy with 2 servers

So i've came across a cool project and i wanted to recreate it. It is my first time using nginx and also my first time learning things about a reverse proxy. I've currently have a reverse proxy running and it works (I guess). But the Proxy currently only works with other ports. I have 3 servers that are running nginx. I use one of them as my reverse proxy. I can access the other servers with different ports. See here (reverse-proxy.conf):
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
proxy_pass http://192.168.2.20;
}
}
server {
listen 8080;
root /var/www/html;
server_name localhost;
location / {
proxy_pass http://192.168.2.30;
}
}
Are there a way to use the reverse proxy without using different ports? Or is my solution ok? At the end i just need a reverse proxy that is able to communicate with 2 other servers.
So one thing here people use reverse proxy in a different ways
But most generic usecase is redirect using location.
Please find the below example.
server {
listen 80;
root /var/www/html;
server_name localhost;
location /a {
proxy_pass http://192.168.2.20;
}
location /b {
proxy_pass http://192.168.3.20;
}
}
Another is giving weight to each proxy.
Please find the below example
stream {
upstream stream_backend {
server http://192.168.2.20 weight=75;
server http://192.168.3.20 weight=25;
}
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
proxy_pass stream_backend;
}
}
In above 192.168.2.20 will receive 75% of the load and 192.168.3.20 will receive 25% of the load. In case if you want to distribute the equal load to both(or round-robin method) Please remove the weight.
I think you may not understand how Nginx work about proxy.
Nginx can reverse Proxy L7 http or L4 stream
and you set the proxy listen on any port or URL you want and proxy to any server or port or URL you want.
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
proxy_pass http://192.168.2.20:2323/URL;
}
}
server {
listen 8080;
root /var/www/html;
server_name localhost;
location / {
proxy_pass unix:/tmp/backend.socket;
}
}
Here is a reference for you about the proxy_pass directive.
proxy_pass

how a single ip mapped to multiple domain names?

I am facing an issue while working on a project. For example i had a ubuntu running with nginx configuration. I had configure wildcard with nginx like:-
main domain : xyz.com
wildcard : a.xyz.com, b.xyz.com working.
The above config is working with https with the help of let encrypt.
Here is my problem, that i am facing right now.
I want that if i give someone a.xyz.com, and then that someone want that a.xyz.com will listen that www.someone.com. Is that possible or not this is the question i am lokking into and if yes then please suggest how??
And if you think question header is nothing to do with my question then let me clear that the same issue i am facing shopify guys are doing some how.
Thanks for your precious time.
You can below configuration
server {
listen 80;
listen 443 ssl;
server_name a.xyz.com;
ssl_certificate /path/to/cert/YourCert.pem;
ssl_certificate_key /path/to/cert/privkey.pem;
location / {
proxy_pass https://www.someone.com;
}
}
server {
listen 80;
listen 443 ssl;
server_name b.xyz.com;
ssl_certificate /path/to/cert/YourCert.pem;
ssl_certificate_key /path/to/cert/privkey.pem;
location / {
proxy_pass https://www.someone2.com;
}
}

NGINX passing requests to Pylons and relative URLs

I have NGINX running on port 8080. I have the following setup in my NGINX conf file.
server {
listen 8080;
server_name domain.com;
location / {
#root /usr/share/nginx/html;
#index index.html index.htm;
proxy_pass http://127.0.0.1:80;
proxy_redirect http://127.0.0.1:80 http://domain.com;
}
These rules work correctly as far as I can tell. The only issue I run into is when Pylons gets a request for a relative URL it is using http://127.0.0.1/linkto/something instead of http://domain.com:8080/linkto/something. I believe I am missing something in my Pylons configuration, if you have any advice or need additional information just let me know. Thanks in advance for any assistance on this.
By default, proxy_pass uses the hostname from the directive (127.0.0.1 in your case) as the Host: header for its request. You probably just need to add proxy_set_header Host $http_host; to have it pass through the original Host header to your backend.

Resources