Deploying multiple Go applications using Nginx - nginx

The are two web applications (websites) written on Go. One is turalasgar.pro (here I am using Go built-in server). Another is engossip.com (for now it displays the same ip as former). I have a vps. I know I should use Nginx, but have no idea how? I have heard of Caddy. Please, I need only nginx server, not Caddy. What I need is run two (or more) applications by using my same vps. How should I configure Nginx configuration? Whether by listening to different ports or to the same port. Practical advices and examples highly appreciated.

It's called reverse proxy. Each application uses it's own port to listen. And then you just point to them in nginx config:
server {
listen 80;
server_name turalasgar.pro;
location / {
proxy_pass http://localhost:8080;
...
}
}
server {
listen 80;
server_name engossip.com;
location / {
proxy_pass http://localhost:8081;
...
}
}

Well is really easy.
follow this guide:
https://www.digitalocean.com/community/tutorials/how-to-use-martini-to-serve-go-applications-behind-an-nginx-server-on-ubuntu
After you achieved one application working with martini+nginx just add another server block for the other app.
In case you need more information about server blocks:
https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts

Above solutions I tried but didn't work for me
https://gist.github.com/soheilhy/8b94347ff8336d971ad0
server {
listen ...;
...
location / {
proxy_pass http://127.0.0.1:8080;
}
location /blog {
rewrite ^/blog(.*) /$1 break;
proxy_pass http://127.0.0.1:8181;
}
location /mail {
rewrite ^/mail(.*) /$1 break;
proxy_pass http://127.0.0.1:8282;
}
...
}

Related

Nginx rewrite with proxy_pass

I recently have a requirement in Nginx to rewrite a URL and then forward this onto to another backend server to a dynamic proxy pass address. I've tried a few things but have not had much luck at the moment. For example, this is the kind of setup I have in my nginx.conf file:
server {
listen 443;
server_name scheduler.domain-name;
rewrite ^scheduler(.*)/(.*)/(.*) $2$1$3; # scheduler.domain.local/changepass/report?target=service
...
location / {
proxy_pass $to-rewrite-address:9443; # changepass.domain.local/report?target=service
...
}
Essentially, I just need to use a re-written URL variable to forward the request on a different port but can't seen to get this to work.
I've done quite a bit of searching but haven't found the solution to this as of yet, though I understand that the DNS resolver has to be set when using a proxy pass variable (Dynamic proxy_pass to $var with nginx 1.0).
Grateful if anyone could advise on how the above could be achieved, many thanks.
Assuming your endpoint would always be specified as the first part of your URI, here is an example of configuration that should work:
server {
listen 443;
server_name scheduler.domain-name;
resolver <your resolver for domain.local>;
...
location ~ ^/(?<endpoint>changepass|endpoint2|endpoint3|...)(?<route>/.*) {
proxy_pass http://$endpoint.domain.local:9443$route;
}
}
I'm using named capturing groups here for a better readibility, this location block is equal to
location ~ ^/(changepass|endpoint2|endpoint3|...)(/.*) {
proxy_pass http://$1.domain.local:9443$2;
}
I'm not sure if query arguments would be preserved with such a construction, if they won't, change
proxy_pass http://$endpoint.domain.local:9443$route;
to
proxy_pass http://$endpoint.domain.local:9443$route$is_args$args;

Nginx microservice gateway config within kubernetes cluster

Basically we have a set of microservices we have deployed to a kubernetes cluster hosted in AWS. We would like to run these through a gateway configuration in nginx.
Our current configuration which doesn't work looks something like this-
upstream some-api1 {
server some-api1:80;
}
upstream some-api2 {
server some-api2:80;
}
upstream some-api3 {
server some-api3:80;
}
server {
listen 80;
server_name gateway.something.com;
location /api1 {
proxy_pass http://some-api1;
}
location /api2 {
proxy_pass http://some-api2;
}
location /api3 {
proxy_pass http://some-api3;
}
}
Our services have been built with dotnet core, so the underlying urls would be something like http://some-api1/{api/controllername} . I'm always getting a 404 when I try hitting these endpoints through postman, which tells me it can't resolve these mappings.
However I'm able to access an api within the cluster using an explicit config for an api like so(which is what I don't want to do)-
server {
listen 80;
server_name someapi1.something.com;
location /{
proxy_pass http://some-api1;
}
}..
If someone could shed some light on what's wrong with the configuration or recommend the best approach for this it would be greatly appreciated.
As #Luminance suggests, you're seeing traffic to /api1 go to some-api1/api1 instead of just some-api1 on the base path for that target service (which is what your app would respond to). Following https://gist.github.com/soheilhy/8b94347ff8336d971ad0 you could re-write that target like
location /api1 {
rewrite ^/api1(.*) /$1 break;
proxy_pass http://some-api1;
}

Elixir/Phoenix umbrella app with two apps and access through subdomain mapped through reverse proxy with nginx

It's several hours I'm trying but I don't know what I'm doing wrong. I'm on a server with ubuntu 16.04, I have an umbrella app with two apps in it, which I would like to map to frontend.example.com and backend.example.com. On the DNS I have a wildcard for the whole domain (*.example.com + #.example.com). An additional issue is that one of them has to be on HTTPS,so I have the following onstart:
09:45:38.013 [info] Running FrontendWeb.Endpoint with Cowboy using http://0.0.0.0:91
09:45:38.055 [info] Running FrontendWeb.Endpoint with Cowboy using https://0.0.0.0:445
09:45:38.199 [info] Running BackendWeb.Endpoint with Cowboy using http://0.0.0.0:90
I would like Frontend to be mapped to frontend.example.com and the like for the second one. I have this servers.conf under /etc/nginx/conf.d:
server {
listen 80;
server_name backend.example.com;
location / {
proxy_pass http://localhost:90;
...
}
server {
listen 80;
server_name frontend.example.com;
location / {
proxy_pass http://localhost:91;
...
}
server {
listen 443;
server_name frontend.example.com;
location / {
proxy_pass http://localhost:445;
...
}
Is there something missing? It's not really working, only the backend redirect partially works.
EDIT: Should I put something in sites-available too? or the conf file is enough?

Nginx, Pairing Client and Upstream Ports

I wonder if there is a simple way (simple = nginx conf without any lua/perl extension) to achieve the following.
Given the following upstream server and listeners:
upstream backend{
server 1.2.3.4:9080;
server 1.2.3.4:9081;
server 1.2.3.4:9082;
}
server {
listen 8080;
listen 8081;
listen 8082;
...
proxy_pass backend;
}
The requirement is that all traffic that is connected to a given port, will be passed via proxy_pass to the equivalent port at upstream.
Perhaps the upstream would not be used in this case, rather I shall use $http_port or similar, any advise will be appreciated.
TL;DR: ngx_http_upstream_module isn't designed this way.
While it's possible by using "route method" for session affinity, such approach will bring you to very unclear config.
Consider using multiple server blocks. Something like:
server {
listen 8080;
location / {
proxy_pass http://1.2.3.4:9080;
}
}
server {
listen 8081;
location / {
proxy_pass http://1.2.3.4:9081;
}
}
# ...and so on
The solution I have ended up with was using Lua to set a variable (upstream_port) which then used as:
proxy_pass http://$upstream_host:$upstream_port$request_uri

Nginx: How to rewrite only when all location blocks fail?

I have a web site which needs to be using https connection for pretty much the whole site except a few locations which I need to be served via http. To do that I have two servers setup in nginx config. One is for non-secure and one for secure connections. However for the non-secure server, I want to be able to rewrite to the secure web one only when none of the location blocks are validated.
Is that possible? If yes, how?
Structure of my nginx config:
server {
listen 80;
...
location /foo1 { ... }
location /foo2 { ... }
# i can't get this rewrite to work only when all location blocks fail
rewrite ^/(.*) https://foo.com/$1 permanent;
}
server {
listen 443;
...
}
Thanx
Add to the end of the server block:
location / {
rewrite ^/(.*) https://foo.com/$1 permanent;
}

Resources