nginx proxy_pass application with prefix - nginx

I need to serve multiple instances of same application for different users.
Say I have users as user1, user2 and user3. My nginx.conf will be like below.
server {
listen 80;
server_name localhost;
location /user1/ {
proxy_pass http://myapp1;
}
location /user2/ {
proxy_pass http://myapp2;
}
location /user3/ {
proxy_pass http://myapp3;
}
}
The application will redirect user back and forth several times. The userX prefix is lost at first proxy pass and next calls are sent to /.
I am using nginx inside a docker container and already read and tried below.

I simply followed a workaround as below to get done what I needed.
upstream user1 {
server myapp1;
}
upstream user2 {
server myapp2;
}
upstream user3 {
server myapp3;
}
server {
listen 80;
server_name localhost;
location / {
//Used a lua script to identify the user
proxy_pass http://$userX;
}
}

Related

How can I make nginx redirect subdomain to a folder?

How can I make nginx redirect all the requests to my subdomain to a folder?
Example:
http://sub2.sub1.domain.com/
that should indicate that sub2 is a folder in sub1.domain.com/sub2
How can I do this?
The main objective is to hide the folder to the user. So, it should continue as
http://sub2.sub1.domain.com/
My wish is to use a wildcard in sub2.
UPDATE:
I've tried:
server {
listen 80;
listen [::]:80;
server_name ~^(.*)\.sis\..*$;
location / {
proxy_pass http://sis.mydomain.com/$1$request_uri;
}
}
but it also didn't work, any error?
In the nginx directives for sub2.sub1.domain.com you'd put:
server {
listen 80;
server_name sub2.sub1.domain.com;
location / {
proxy_pass https://sub1.domain.com/sub2;
}
}
So any request going to sub2.sub1.domain.com gets proxied to → sub1.domain.com/sub2 (while masked as sub2.sub1.domain.com); no need for a redirect or rewrite this way either.
Wildcard Method
server {
listen 80;
server_name ~^(.*)\.sub1\.domain\.com;
location / {
proxy_pass https://sub1.domain.com/$1;
}
}
*the wildcard method above is untested.

Ngrok not tunneling properly Nginx

I have my flask application deployed on Nginx over my VM.
Everything is deployed Ok and I can request my apis on http://my.ip.number (I have a public IP)
But when I run Ngrok (I need https and I don't have a domain name to generate a SSL certificate), the URL https//number.ngrok.io shows me the Nginx home page (Welcome to Nginx) instead my webapp.
Why is this happening?
P.D: When I run "curl localhost" I get the Nginx Welcome Page but when I exec "curl -4 localhost" I get my webapp home page
etc/nginx/site-available/myproject
server {
listen 80;
server_name 0.0.0.0;
location / {
include proxy_params;
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name public.ip;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
Any request coming in from ngrok, has the Host header set to the ngrok URL. The behaviour of nginx would be to try and match one of the server blocks in your configuration above, and default to the first one if no server_name matches the Host header.
However, I'm guessing there's another configuration file at /etc/nginx/conf.d/default.conf or /etc/nginx/sites-enabled/0-default which has a listen directive with default_server set. That will be catching these requests and serving the "Welcome to nginx!" page.
I suggest you look for that file, and remove it which should solve the issue.
However you could also simplify the above configuration and simply have:
server {
listen 80;
server_name localhost;
location / {
include proxy_params;
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
Provided there's not another server block hiding somewhere else in the configuration with a directive like listen 80 default_server; then this should catch all requests.
For more info see: How nginx processes a request

nginx reverse proxy between 2 https servers

I'm a bit new to using nginx so I'm likely missing something obvious. I'm trying to create an nginx server that will reverse proxy to a set of web servers that use https.
I've been able to get it to work with one server list this:
server {
listen $PORT;
server_name <nginx server>.herokuapp.com;
location / {
proxy_pass https://<server1>.herokuapp.com;
}
}
However, as soon I try to add in the 'upstream' configuration element it no longer works.
upstream backend {
server <server1>.herokuapp.com;
}
server {
listen $PORT;
server_name <nginx server>.herokuapp.com;
location / {
proxy_pass https://backend;
}
}
I've tried adding in 443, but that also fails.
upstream backend {
server <server1>.herokuapp.com:443;
}
server {
listen $PORT;
server_name <nginx server>.herokuapp.com;
location / {
proxy_pass https://backend;
}
}
Any ideas what I'm doing wrong here?

Why is my nginx "rewrite" directive causing a redirection loop?

Given the following http block, nginx performs as expected. That is, it will rewrite a URL such as http://localhost/3ba48599-8be8-4326-8bd0-1ac6591c2041/ to http://localhost/modif/3ba48599-8be8-4326-8bd0-1ac6591c2041/ and pass it to the uwsgi server.
http {
upstream frontend {
server frontend:8000;
}
server {
listen 8000;
server_name localhost;
root /www/;
location ~* "^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})/?$" {
include uwsgi_params;
set $uuid $1;
if ($cookie_admin) {
# if cookie exists, rewrite /<uuid> to /modif/<uuid> and pass to uwsgi
rewrite / /modif/$uuid break;
uwsgi_pass frontend;
}
content_by_lua_block {
ngx.say("Ping! You got here because you have no cookies!")
}
}
}
}
However, when I add another location block in the manner displayed blow, things fall appart and I get ERR_TOO_MANY_REDIRECTS.
http {
# access_log /dev/stdout; # so we can `docker log` it.
upstream frontend {
server frontend:8000;
}
server {
listen 8000;
server_name localhost;
root /www/;
location / { # THIS MAKES EVERYTHING FALL APART :(
uwsgi_pass frontend;
include uwsgi_params;
}
location ~* "^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})/?$" {
include uwsgi_params;
set $uuid $1;
if ($cookie_admin) {
# if cookie exists, rewrite /<uuid> to /modif/<uuid> and pass to uwsgi
rewrite / /modif/$uuid break;
uwsgi_pass frontend;
}
content_by_lua_block {
ngx.say("Ping! You got here because you have no cookies!")
}
}
}
}
What's going on here, exactly? How can I fix this?
I see your Nginx is listening on port 8000, but your upstream server is at 'frontend', also on port 8000. If frontend resolves to the same server that Nginx is running on, then you've got a loop of proxy requests happening.

How can i pass subdomain as proxy_pass value in nginx?

i am currently in situation where i need to get/catch sub-domain and pass that sub-domain value to proxy_pass in Nginx config.
e.g.
if user enters
http://google.com.mydomain.com
then it should do proxy pass as
proxy_pass http://www.google.com/;
in above example google.com is sub-domain
can this be doable ?
How can i achieve something like that in nginx ?
currently i am using configuration where sub-domain values are hard-coded in config files , but there are many sub-domains , so i need to do it like this way, but don't know correct syntax.
server {
listen 80;
server_name subdomain.domain.com;
charset utf-8;
location / {
proxy_pass http://www.subdomain/;
}
}
I am using * as A record to redirect all sub-domains to my web-host, i.e. wildcard DNS.
update:
i have found code snippet from https://stackoverflow.com/a/22774520/1642018
server {
listen 80;
# this matches every subdomain of domain.
server_name .domain.com;
location / {
set $subdomain "";
if ($host ~* "^(.+)\.domain.com$") {
set $subdomain $1;
}
proxy_pass http://$subdomain;
}
}
but the request is showing my default page which is in my default web root.
Two things.
1- A resolver (a dns server for your nginx in order to resolve google.com, you may add at your hosts or you are able to add resolver statement)
2- You will need to resolve how your client will deals with different domains, I mean google.com is different than google.com.ar or google.fr)
At this example I made work it for your example google.com
worker_processes 4;
error_log /var/log/nginx/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 80;
location / {
set $subdomain "";
if ($host ~* "^(.+)\.domain.com$") {
set $subdomain $1;
}
resolver 8.8.8.8;
proxy_pass "http://$subdomain.com";
}
}
}
I hope that this configuration helps to you.
I would capture the subdomain using a map then proxy pass if the variable is defined:
map $host $subdomain {
~^(?<sub>.+)\.[^\.]+\.[^\.]+$ $sub;
}
server {
listen 80 default_server;
server_name _;
location / {
if ($subdomain) {
proxy_pass http://$subdomain;
}
}
}

Resources