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;
}
}
}
Related
Here is a URL : http://123.com/stat/api/abc?a=123
if I want to set proxy pass to : http://456.com/api/abc?a=123
My nginx config as below :
server {
listen 80;
server_name 123.com;
location /stat {
proxy_pass http://456.com;
}
}
And this is not work.
Any ideas?
Finally, I found the solution.
The final config as below :
location /stat/ {
proxy_pass http://456.com/;
}
the end of slash in location section and proxy_pass section must be have.
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.
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;
}
}
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 serve multiple domains with the same configuration, without copying the server{} rule configuration for every domain?
example.com
example.org
example.de
example.ro
Nginx Config:
upstream example_live {
server 127.0.0.1:8300;
}
server {
listen 80;
server_name example.com example.org example.de example.ro;
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
location / {
proxy_pass http://example_live/VirtualHostBase/http/example.??:80/example/VirtualHostRoot/;
include /etc/nginx/ps.cfg/proxy.conf;
}
include /etc/nginx/cfg/base.conf;
}
in the same server section and using server_name directive to support multiple domains. And seems you already give the answer above.
This works for me, thank you for your Comment
server_name a.com www.a.com
b.org www.b.org
c.net www.c.net;
access_log /var/log/nginx/a.com.access.log;
error_log /var/log/nginx/a.com.error.log;
location / {
rewrite ^(.*)$ /VirtualHostBase/http/$http_host:80/a/VirtualHostRoot$1 break;
proxy_pass http://127.0.0.1:8080;
}