Run 3 websites on one domain using nginx - nginx

Lets say i have domain abc.com which redirects my server ip 1.2.3.4
i have 3 websites running on my server using docker container,
1st website ip 1.2.3.4:50/a
2nd website ip 1.2.3.4:60/b
3rd website ip 1.2.3.4:70/c
i can access this all websites using ip
i want to use my domain to run these three websites.
e.g.
abc.com/a should run 1st website
abc.com/b should run 2nd website
abc.com/c should run 3rd website
i am using nginx server version 1.14.1 on RHEL
Please help
i tried these configuration file
location /a {
rewrite ^/a(.*)$ http://1.2.3.4:50/a redirect;
}
location /b {
rewrite ^/b(.*)$ http://1.2.3.4:60/b redirect;
}
but it is redirecting and displaying the port in the url

Try this to solve your problem
location /a {
proxy_pass http://1.2.3.4:50;
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 /b {
proxy_pass http://1.2.3.4:60;
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 /c {
proxy_pass http://1.2.3.4:70;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
For your information, this proxy configuration will make your abc.com/a to access the root of your http://1.2.3.4:50 website. But the /a will also be passed to the http://1.2.3.4:50 so you don't have to bother add http://1.2.3.4:50/a for each proxy
You can configure it explicitly inside the nginx.conf
// Nginx.conf
http {
...
server {
server_name abc.com;
// here is location ...
}
}
Or by including configurations in sites-available, usually in the nginx.conf you can found this section:
// Nginx.conf
http {
...
include /etc/nginx/sites-available/*;
...
}
Then you can add site.conf or main.conf, you name it yourself for example in sites-available folder that has this kind of configuration:
server {
server_name abc.com;
// here is location ...
}

Related

nginx reverse proxy multiple external sites hosted on different port to same port, different subdomain?

This may have been answered many times before but most answers are site specific so wanted some insight on a bareboned nginx config on how to redirect multiple external sites under same server but different subdomain. Pls note the external sites are inaccessible and need the reverse proxy via XYZ to make accessible.
I found existing nginx config already a proxy set up for site1 : AAA proxied through http://XYZ:8088. Below is the existing config. Now, how do I go about adding another site2 to be proxied via http://XYZ:8088/site2
So far, I tried to add additional section at the bottom of config similar to site1 (which is perfectly working fine), however site2 css/images is lost if I try to hit http://XYZ:8088/site2
server {
listen 8088 default_server;
server_name "";
return 444;
}
server {
listen 8088;
server_name "XYZ.example.com";
charset utf-8;
# Deny access to .htaccess files.
location ~ /\.ht {
deny all;
}
# Proxy to site1 server.
location / {
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name:$server_port;
proxy_pass http://site1;
}
# Proxy to site2 server.
location /site2 {
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://site2/;
}
}
upstream site1 {
server site1:1111;
}
upstream site2 {
server site2:2222;
}
Any help would be appreciated. Also if someone can explain why below config is behaving this way ? I understand it has something to do with the additional "/site2" being used now. But how do I make it ignore that.

Nginx route my localhost port requests to external url

I was trying to route my localhost:8585 requests to some external url, say https://someExternalSite.xyz. Such that i can access the external site using localhost port url in my app.
i.e if i curl localhost:8585, it should actually return the output of curl https://someExternalSite.xyz
I have tried this configuration but not working
server {
listen 8585;
listen [::]:8585;
server_name localhost;
location / {
proxy_pass https://someExternalSite.xyz/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
}
}
Need some help

I want to host a static website on EC2 with nodejs app too

I am trying to host a static website on EC2 but no luck.
here is my config file node
server {
listen 80;
server_name localhost;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:3000";
}
}
I want to host static website too.
How can I do that on EC2
I'm not sure how I can explain it end to end. I hope you got a basic idea of how it works.
From your question, I can understand that you are having some problems with the Nginx configuration.
your Nginx config file should look like this,
location / {
# This would be the directory where your frontend code resides
root /var/www/html/;
try_files $uri /index.html;
}
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000/;
proxy_set_header Host $http_host;
proxy_redirect off;
}
You can use PM2 for running the nodejs app in your VM.
Here Nginx would be webserver for your frontend application and a proxy to your backend application, all the request is going to hit on your Nginx server.
I hope this is what you are looking for.

Nginx directs request to www.example.com but not to internal web application

I've installed several web applications on different ports on the same server. From that server when I send an http request using wget or curl the request goes through and I get the response. I've set up nginx server to not have to specify the port each time. Here's the related nginx config:
server {
listen 10.0.223.34:80;
server_name app1.domain.com;
access_log /var/log/nginx/app1.domain.com.access.log;
error_log /var/log/nginx/app1.domain.com.error.log;
location / {
proxy_pass http://10.0.223.34:8080;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
If I try app1.domain.com from outside I get 502 Bad gateway error. But if I change the proxy_pass to http:\\www.example.com, then nginx takes me to the example.com website.
Inside the nginx.conf file I've specified user nginx;. I've tried changing it to root but it didn't help either. Do you have any idea what else I need to check?
Try this:
upstream app1 {
server localhost:8080;
}
server {
listen 10.0.223.34:
server_name app1.domain.com;
access_log /var/log/nginx/app1.domain.com.access.log;
error_log /var/log/nginx/app1.domain.com.error.log;
location / {
proxy_pass http://app1;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

Nginx proxy pass configuration docker

Here's the problem:
The host machine has multiple docker apps running on different ports for eg. App1 # 3001, App2 # 3002...3100 etc
Now I would like to access the apps in this format http://hostname.com/app1, http://hostname.com/app2..
To do this i'm running nginx on the host to proxy requests to the right port based on the sub-uri
location = /app1 {
proxy_redirect http://hostname:3001/;
include /etc/nginx/proxy_params;
}
location ^~ /app1 {
proxy_redirect http://hostname:3001/app1;
include /etc/nginx/proxy_params;
}
But this does not work when the site's sub uri changes or if the site redirects.
For example:
If I visit the site at hostname:3001 -> I can see the site
If I visit the site at http://hostname.com/app1 -> I can see the site
If the site page is at hostname:3001/static/index.html then when i access it as http://hostname.com/app1 the page changes to http://hostname.com/static/index.html -> I get 404.
Is there a way to do this? Or is the only way to do it is to set the dns as app1.hostname.com and do a name based routing?
Inside your server {} block you want:
location /app1 {
rewrite ^/app1(.*) /$1 break;
proxy_pass http://hostname:3001/;
proxy_redirect off;
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 /app2 {
rewrite ^/app2(.*) /$1 break;
proxy_pass http://hostname:3002/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
The rewrite rule here will pass the correct uris to the ports
You can make every app listens on a separate port (e.g. 3000 and 3001) then configure your nginx as follows (include it inside the server {} definition block):
location /app1 {
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
}
location /app2 {
proxy_pass http://localhost:3001;
proxy_set_header X-Real-IP $remote_addr;
}

Resources