NGINX - Proxy (I think) - nginx

so I have 4 instances of node running for 4 different sites. I want to use Nginx, here is what I want.
www.site1.co.uk
= localhost:50010
www.site2.co.uk
= localhost:50020
www.site3.co.uk
= localhost:50030
test.site3.co.uk
=localhost:50031
I'm not asking for the code, (although it would be nice), what is the best way to achieve this sort of setup? I am still new to Nginx so sorry if it's a really simple question.
Thanks in advance,
Harry

To proxy the requests to those ports according to the domain being requested use this
server {
server_name www.site1.co.uk
proxy_pass http://localhost:50010
}
server {
server_name www.site2.co.uk
proxy_pass http://localhost:50020
}
server {
server_name www.site3.co.uk
proxy_pass http://localhost:50030
}
server {
server_name test.site1.co.uk
proxy_pass http://localhost:50031
}

Related

how to split traffic from same url path to 2 different upstreams?

i have these upstreams declared:
upstream upstream_1 {
server some_container_1:8000;
}
upstream upstream_2 {
server some_container_2:8001;
}
and this server:
server {
listen 7000;
server_name localhost;
location /path {
uwsgi_pass upstream_1;
}
}
where both some_container_1 and some_container_2 are based on same image (thus offer the same apis on the same paths) but differ on env vars and other non related stuff. i want to fork 1% of all traffic from localhost:7000/path to be delivered 'as is' to upstream_2 and 99% to remain on upstream_1. both cases should keep the request as received, altering neither path nor headers
with split_clients i can fork which path will be set before forwarding the request to a single upstream, which is not my case.
here the fork is done inside an upstream between servers, not inside a location splitting between upstreams, as i need.
can i define an upstream of upstreams like
upstream compound_upstream_1 {
upstream upstream_1 weight=99;
upstream upstream_2;
}
to use it on
server {
listen 7000;
server_name localhost;
location /path {
uwsgi_pass compound_upstream_1;
}
is it possible to do this with nginx? considering so, which way should be the standard to accomplish this?
I don't understand, what stops you from using server names in the upstream block directly?
upstream compound_upstream_1 {
server some_container_1:8000 weight=99;
server some_container_2:8001;
}
server {
listen 7000;
server_name localhost;
location /path {
uwsgi_pass compound_upstream_1;
}
}
Or maybe I misunderstand your question?
It might be possible to accomplish this using a load balancer: https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/
I'm not sure what the weights would be for your '1%' scenario but you can toy with it and adjust it to your liking.

Nginx location part not being replaced in proxy_pass when using variable

I have a service listening on myservice.mycompany.local
We're proxifying request like this
server {
listen 80;
location /myservice/ {
proxy_pass http://myservice.mycompany.local/;
}
}
it all works fine requests on public.mycompany.com/myservice/api/1/ping are correctly transformed into request to http://myservice.mycompany.local/api/1/ping as there is the trailing /
but now if we try to use a variable
server {
listen 80;
set $MY_SERVICE "myservice.mycompany.local";
location /acm/ {
proxy_pass http://$MY_SERVICE/;
}
}
the local service will only receive a requests to / with the URI part being lost
I've been able to reproduce this "problem" with several version of nginx
1.8.1-1~wheezy
1.4.6-1ubuntu3.5
I'm able also to reproduce it locally by replacing the proxified service by a simple nc -l 127.0.0.2 8080 and using it as the value of my variable, so it really seems to be something happening inside nginx
And this behaviour is not covered in http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
You may have discovered an undocumented feature, but you can always use a rewrite ... break instead of proxy_pass aliasing:
server {
listen 80;
set $MY_SERVICE "myservice.mycompany.local";
location /acm {
rewrite ^/acm(/.*)$ $1 break;
proxy_pass http://$MY_SERVICE;
}
}

Multiple domains - subdomains routed to main domain

I have multiple domains connected to the same DO droplet, with nginx
Let's assume:
firstdomain.com
seconddomain.com
I would like to set my nginx in a way that, every subdomain will be directed to it's main domain, and it will be reflected in address bar too:
subdomain.firstdomain.com ----> firstdomain.com
asldk.firstdomain.com --------> firstdomain.com
test.seconddomain.com --------> seconddomain.com
and so on.
What is the simplest way to achieve this?
server {
listen 80;
server_name ~^(?<subdomains>.+\.)?(?<domain>[^.]+\.[^.]+)$;
if ($subdomains != "") {
rewrite ^/(.*)$ http://$domain/$1;
}
}
I want to post this answer, for future visitors and for myself (I'm sure I will forget)
server {
listen 80 default_server;
server_name ~^(?<subdomains>.+\.)?(?<domain>[^.]+\.[^.]+)$;
if ($subdomains != "") {
rewrite ^/(.*)$ http://$domain/$1;
}
index index.html;
root /etc/nginx/conf.d/404;
}
It is almost identical with the solution I accepted, and it has a defined 404 page in conf.d to modify default nginx page.

How to look for a certain header in nginx

I'm using nginx as a proxy server to basically serve image files and hand off everything else to another server at port 9000.
What I want to do is have nginx return an HTTP 500 error code if the incoming request does not contain a specific header (X-AUTH-TOKEN), but only if the requests are not for the resources "/register" or "/events". In that case, they need to go straight to port 9000.
Here's the configuration I currently have:
http {
sendfile on;
upstream my-backend {
server 127.0.0.1:9000;
}
# main ngingx server
server {
server_name localhost;
location / {
proxy_pass http://my-backend;
}
location /images/ {
root /home/images;
}
}
}
Any idea how to implement this kind of logic? Thanks.
There is native HttpCoreModule feature exists:
You can check any header exists/match by something like this:
location /blah {
if ( $http_x_auth_token ) {
// do something, allow
}
}
http://wiki.nginx.org/HttpCoreModule#.24http_HEADER
Hope it will help :)

How to use nginx as reverse proxy to direct localhost:9292 to a sub domain foo.localhost/?

I've learned how to pass localhost:9292 to localhost/foo with the following directive:
location /foo {
proxy_pass http://localhost:9292;
}
but I want do something like
foo.localhost -> localhost:9292
Is there a way I can do that?
If foo.localhost is your sub domain name and you want to proxy pass sub-domain to main-domain, you can use proxy_pass and you can learn a little more about server directive if needed. An example:
server {
listem 8080;
host sub.main.com;
...
location / {
proxy_pass http://main.com;
break;
}
}
server {
listen 8081;
host main.com;
...
location / {
//do something
}
}
This is proxy pass, means when access sub.main.com, actually it finally dealt by main.com, but the client side still shows sub.main.com. If you want client side shows main.com, here should use redirect but not proxy_pass.

Resources