nginx manage local servers depends on income domain name - dns

I have a few applications that are running localy in defferents ports, how can I configure NGINX server for forwarding request from port 80 to my application depends on income domain name. For example 2 local apps named 'app1' on port 8181 , and if request comes from http://app1.com - nginx forwards to http://localhost:8181
I've looked at nginx docs, I ask for your examples if someone did this.
Thanks

Assuming you want to create a reverse proxy, my method is to first configure the following reverse proxy settings in a new file called /etc/nginx/reverse-proxy.conf:
# Serve / from local http server.
# Just add the following to individual vhost configs:
# proxy_pass http://localhost:3001/;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
Then, for each reverse proxy I'm configuring, I add an appropriately-named configuration file in /etc/nginx/sites-enabled with the following:
server {
server_name app1.com;
server_name www.app1.com;
location / {
include /etc/nginx/reverse-proxy.conf;
proxy_pass http://localhost:8181/;
}
}
You can create as many server blocks as you like and point them at different local (or even remote) application servers. You can also add location blocks to serve different URLs within the same domain statically, or from different local application servers.
(You can also just roll all the configuration into /etc/nginx/nginx.conf, but I find it easier to separate configuration out into multiple files.)

I managed to do this easily by following this tutorial.
Create a new file in /etc/nginx/conf.d/ called your-domain.com.conf and put this in it:
server {
listen 80;
server_name your-domain.conf.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2368;
}
}
Then restart nginx
sudo service nginx restart

Related

How to configure Nginx as reverse proxy to do this network schema?

I have two Docker instances running Wordpress and my Nodejs application separately in the same machine.
Now I want to set up Nginx to accept connections only on my domain (mydomain.com). So if users access to mydomain.com/ they should be redirected to wordpress container, and if they access to mydomain.com/customer, the request should be redirected to Nodejs app container.
This schema explains the idea:
I am not very good at Nginx and it is being so difficult for me to edit the config. file to match that schema. Also I dont find any kind of examples or documentation to achieve this configuration. Is it so weird?
It is the very common nginx usage example. The very basic setup you need is
server {
listen 80;
server_name mydomain.com;
proxy_set-header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://<docker1_address:port>;
}
location /customer {
proxy_pass http://<docker2_address:port>;
}
}
server {
# this would be the default server for all requests
# where HTTP Host header is missing or not equal to 'mydomain.com'
listen 80 default_server;
# close the connection immediately
return 444;
}

ElasticBeanStalk NGinx configuration for multiple ports in Nodejs

I have my nodejs hosted in ElastiCbean stalk environment.
It uses the default configurations and default port.
Now I am planning to open another port and listen to that port from Nodejs applicaiton. This is kind of opening nodejs in mutliple ports.
I have done the nodejs coding part.But i am not sure of the nginx changes to make it to listen to multiple ports.
Can someone explain it to me?
I've only configured nginx for Java backends, but essentially you will need to configure the server directive to include the additional port you want to listen on, such as:
server {
# listen for the extra the port the load balancer is forwarding to
listen 88;
access_log /var/log/nginx/access.log main;
client_header_timeout 60;
client_body_timeout 60;
keepalive_timeout 60;
gzip off;
gzip_comp_level 4;
location / {
# forward to the actual port the application runs on
proxy_pass http://127.0.0.1:8888;
proxy_http_version 1.1;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
So I recommend ssh on to your Nodejs EB server and fish around for your nginx config directories, looking for nginx, conf.d folders, or
an nginx.conf file. When you find it, you can override the default server config, or apply an include statement to extend it, either way the server directive above should allow access on multiple ports as far as nginx is concerned.

multiple node js app with different domain in same vps

I have a Digitalocean VPS and 3 Nodejs (ExpressJs) application. Now I started all of them in same IP and different port like this:
- 95.5.5.8:65001
- 95.5.5.8:65002
- 95.5.5.8:65003
But now I want when anybody writes app1.com, the first app 95.5.5.8:65000 shows to him like this:
app1.com -> load server in port 65001
app2.com -> load server in port 65002
app3.com -> load server in port 65003
All of the applications are in same VPS by the same IP, How can I do this, please?
You can implement this feature by following steps:
In domain setting page, map app1.com, app2.com and app3.com to 95.5.5.8.
In Nginx, configure 3 proxies for app1.com, app2.com and app3.com in directory /etc/nginx/conf.d/. Here is the proxy file /etc/nginx/conf.d/app1.conf for app1.com:
server {
listen 80;
server_name app1.com;
access_log /var/log/nginx/app1_access.log;
charset utf-8;
location / {
proxy_pass http://95.5.5.8:65001;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
}
}
Restart Nginx.

NGINX dns is not working on VPS

I'm a little new to VPS/Linux so please bear with me.
I have a domain name (attendahh.com) pointed to my host's nameservers.
I've set up /etc/nginx/conf.d/attendahh.com.conf as follows:
# the IP(s) on which your node server is running. I chose port 3000.
upstream attendahh.com {
server 127.0.0.1:1999;
}
# the nginx server instance
server {
listen 0.0.0.0:80;
server_name attendahh.com attendahh;
access_log /var/log/nginx/attendahh.log;
# pass the request to the node.js server with the correct headers and much $
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://attendahh.com/;
proxy_redirect off;
}
}
Then I service nginx restart.
I've read a bunch of tutorials and stack answers this is ostensibly all I need to do, but if I go to http://attendahh.com it does not work.
Things to note:
Going to my IP + port in the browser works just fine ( 23.226.227.16:1999 )
I have Cpanel for VPS installed (I tried to set up the DNS in there originally but it didn't work, I've since deleted the dns entry from there but it may still be affecting things)
Apache virtual hosts are commented out in httpd.config.
Any ideas on what I'm doing wrong here? Perhaps there's some conflict between apache and nginx?
- proxy_pass http://attendahh.com/;
+ proxy_pass http://attendahh.com;
Nginx uses its own resolver, it doesn't use the system's resolver (/etc/resolver).
You have to configure it using the resolver directive. If you are not using the resolver directive, then use IP address in the proxy_pass directive.
Source: http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver

Subdomain problems using nginx as a reverse proxy for node apps

I am new to nginx and I'm struggling to get my configuration for a reverse proxy working. I have a node app running on localhost:3010 and I'm trying to serve pages through nginx from this app at the subdomain dev.[sitename].org. Let's just say dev.example.org for readability. Here are the contents of a file I created in sites-available called example.org (is that the correct name for this file?):
server {
server_name www.example.org example.org;
}
upstream app_dev.example.org {
server 127.0.0.1:3010;
}
server {
listen 0.0.0.0:80;
server_name dev.example.org;
access_log /var/log/nginx/dev.example.access.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_dev.example.org/;
proxy_redirect off;
}
}
This is mostly based off this related question: Node.js + Nginx - What now? however when I try to open dev.example.org in my browser, Chrome reports that it can't find the page. I can ping dev.example.org and get an IP address, so the server seems to be available, but my nginx configuration incorrect. I created the symlink in sites-enabled and restarted nginx, in case you thought I might have forgotten those steps.
So my thought now is that I'm not referring to the subdomain correctly somewhere, or maybe my file in sites-available is named wrong. Any push in the right direction would be appreciated.
Just to be sure the problem is on nginx try these steps:
Set a test server at port 3030, serving the system doc folder or anything else.
server {
listen 3030
location / {
root /usr/share/doc/;
autoindex on;
}
}
upstream simple_test {
server 127.0.0.1:3030
}
Then use simple_test below as well:
proxy_pass http://simple_test/;
If you see the /usr/share/doc dir listing when you access dev.example.org then your issue is on the node side.
Turned out something was blocking port 80! fixed that and the config as posted above worked

Resources