Nginx multiple subdomains in local network - nginx

So this is a common question but with a twist that I have a hard time solving. I want to have multiple apps in a server (a raspberry pi to be exact) in my local network. Not connected to the internet. I don't have and don't want a domain name, however I have set up my router DNS to point from home to the server local IP. All of this works fine and I can access an HTTP server at http://home.
I want to be able to do:
app1.home -> proxy to app1 in 3000
app2.home -> proxy to app2 in 3001
and so on.
I have tried to create one server for each app. If I set server_name to app1.home (for example) it does not work.
I'm kinda lost, I have only set up basic Nginx servers and this is my first complicated one. How should I approach this?

Related

Multiple Web Applications - Same VM vs Multiple VMs

Firstly, I am more of a dev than admin. And I have always asked questions here. But please let me know if there is a better place to ask this question.
Here's my situation. I have an application that is built to run on linux. It serves both https (on port 443 using nginx) and ssh (on port 22). But due to organizational restrictions, I am forced to run it on a windows host with a linux guest using virtual box. Also, there is another web application on the host box; both these web applications should be served based on the URL (example: app1.com, app2.com). URLs need to be preserved. All ssh traffic can default to guest.
One idea I have to make this work is below, and I would like to know
if I am making this more complicated than it should be. Any help is appreciated.
Steps:
Use an unused port for https (say 8443) on my host and redirect all
traffic to the guest. Use NAT based port forwarding (8443 -> 443, 22 -> 22)
in Virtualbox.
The only thing left would be to setup another nginx on
the host as a reverse proxy. Set up virtual hosts on windows
(/etc/hosts) and have the two IP and URL entries (app1.com and app2.com).
Use a separate nginx on the host as a reverse proxy to redirect app1 traffic
to the web app on the host and app2 traffic to 8443.
Questions:
Can I avoid the extra nginx reverse proxy on the host while preserving the URL?
Also what about ssl. Can I just set up https on the host and route it to port 80 on guest and avoid having two certs? Note: I am using NAT in Virtualbox, so there should not be any security issues I guess.
This is an administration question, and the user posted the identical question to serverfault, where it was answered: https://serverfault.com/questions/835220/multiple-web-applications-same-vm-vs-multiple-vms

nginx load balance structure

I have a website that is growing so my dedicated server can't handle and the lag is real deal. So I decided to try cloud hosting and for this purpose I will use nginx as load balancer.
Qustion 1.
If I configure the main webserver where domain.tld is located as load balancer like the example here will I be able to use the same server for all my other domains(right now I use normal nginx config to maintan 10 small websites on the same webserver), or the main role will be ONLY to balance and redirect the traffic.
Question 2.
Shall I put copy of the files on the mirror servers ?
Example : my website is in the http_web folder , where he communicate with MYSQL server. How the requests are handled ? What happens when the balance server redirect the client to the server1 ?
Question 3.
I plan to start with this structure:
Load balancer (dedicated server) + Mysql -> http servers1, server2,,, on demand3..4..5.. Is that ok?
This diagram should help with how you should set it up. Forgive me if I missed your point to your question. So the green is what is public and red is your private internal network. So your load balancer which you want as nginx has two networks connected to it. Your external public network and your internal network. The lb should handle all the ip's coming from your clients. Then nginx delegates the client to one of the webapps through the private internal network. I hope this helps.

How to keep the session when using nginx as the Reverse Proxy to many servers with unsame project

I am new to Nginx. And I have trobule with it. We have many projects with different language and framework. And they are put in different server. How do I keep the session for every project respectively?
Question is not quite clear but from what i understood i will try to guide you a bit...
Nginx is a web server which when used as reverse proxy basically just sits in front of your project appserver. When some client tries to connect to your appserver, it will first connect to nginx and then nginx will forward that request to you appserver.
eg.
client -Req-> nginx (port 8080) -Req-> appserver(jetty, port 9000)
Now if you are trying to use a single nginx instance and direct request to multiple app servers from nginx. You will either have to make nginx listen on different ports and forward them to different appservers. Or nginx can identify which request is meant for which appserver by routes.
Here is a source which can help you to learn how to configure Nginx to do this... please ask again if you need further help.
https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts

Setting up a SRV record to redirect from subdomain to main domain but different port

So this is my desired result.
I have a VPS (hosted at DigitalOcean) that has two servers running. One is a webserver listening on port 80 that can be accessed simply by typing my domain name like myfirefly.me
The other server is also a webserver but listening on port 8442. It can be accessed like myfirefly.me:8442.
What I want is to be able to access it like elopakao.myfirefly.me.
If I understood my google results correctly, the easiest way to do this is using SRV records. DigitalOcean provides support for them but I'm having trouble understanding what I need to type in each of the fields. This is my current setup:
_sip._tcp.myfirefly.me. IN SRV 0 5 8442 elopakao.myfirefly.me.
where myfirefly.me is my domain and elopakao is the desired subdomain.
Apparently the setting has propagated but when I type in elopakao.myfirefly.me I don't get anything.
What I'm assuming is happening is that elopakao.myfirefly.me is being routed to elopakao.myfirefly.me:8442 instead of myfirefly.me:8442
How can I make this work?

Nginx two virtual hosts on with domain name one in localhost

On my Nginx I've got two hosts.
One with the values
server_name = www.mydomain.com;
root /var/www/production/myFirstWebSite;
and the other with
server_name=localhost;
root /var/www/development/mySecondWebSite;
To my domain registrar account I configured the DNS with two A record "
www IN A myIP
IN A myIP
This is cool, i can reach my first website with www.mydomain.com or mydomain.com.
Now the problem is how to reach my second website which is in development and I don't buy the domain name. And myIP/development/myScondWebSite is no more working ...
I think that the problem come from the DNS entries but I'm not sure.
Do you've got some ideas ?
Thanks in advance.
There's a couple of ways I could think of to access the localhost one.
Creating a subdomain instead of localhost
This is the best one I'd recommend, try doing something like server_name localhost.mydomain.com.
If you need to put further security, you could make it only allow a certain IP(s) or a range of IPs.
Play with your hosts file
In this specific case I would not recommend this, because you're messing with localhost it self, might break some other stuff on your machine, if it was any other name I could have said it's fine.
Use an ssh tunnel to the server
In this method you create a dynamic port on your ssh connection and set your browser to pass all traffic through tunnel which goes to the server then it's handled from there, so if you run localhost for example it would be like running localhost from over there, but since this involved a browser setting, you need to remember to disable it after you disconnect the ssh connection otherwise the browser would return an error saying that the proxy server is refusing the connection.
Using a local Nginx as a proxy
This one I just came up with right now, and I can't say If it would work or not, the 3 before I've worked with before and I know they work.
You'd set a certain domain name that your local nginx would capture and then proxy it to the remote server, but edit the host header setting it to localhost instead, that way it would match the localhost in the remote machine, if this one works it would not need any setting to be turned on and off every time.
Out of all these, I'd recommend the first one first (if it's an option), then try the last one if you don't want to keep turning things on and off before and after each setting.

Resources