2

I'm on ubuntu 20.04 rpi4 and I like to write some www site for testing.

Is quite simple configure nginx using some server blocks and server_name inside the server blocs to point to some virtual domain not existing and then set this domain to point to localhost in /etc/hosts:

# /etc/hosts
127.0.0.1 adminer
127.0.0.1 pippo
127.0.0.1 pluto

to have some site like this:
http://adminer
http://pippo
http://pluto

But I like to avoid /etc/hosts setting.
what I like is:
http://localhost/adminer
http://localhost/pippo
http://localhost/pluto
...
to point to 3 different site adminer, pippo and pluto.

It is possible?
what configuration have to use?

can I use one server block for one site or have I to use one server block to all 3 sites?
I'm new on nginx ...

best regards,
Leonardo

  • 1
    https://stackoverflow.com/questions/11570321/configure-nginx-with-multiple-locations-with-different-root-folders-on-subdomain – lotfio Dec 16 '20 at 21:43
  • 1
    ok. so I cannot have multiple server_name localhost and have to use alias for sub site like they are like subdomain. I try and let you know. thank you. –  Dec 17 '20 at 08:20

2 Answers2

2

I just came across the same issue and I used ports to achieve that.

This solution worked for me on a local machine and home network and probably works on any VPS without domain.

WEB SERVER 1

Open your firewall, example port 81

sudo ufw allow 81

Create your 1st web directory

sudo mkdir -p /var/www/web-folder-name1

Create test content in your web-folder

sudo nano /var/www/web-folder-name1/index.html

and paste any content here to test

Hello World 1!

Create a virtual host file in Nginx

sudo nano /etc/nginx/sites-available/web-folder-name1

and paste the following content

server {
    listen 81; # the port is important
    server_name _; # underscore is ok as you don't have a domain
    root /var/www/web-folder-name1;
    index index.html;
}

Enable your web server

sudo ln -s /etc/nginx/sites-available/web-folder-name1 /etc/nginx/sites-enabled/

WEB SERVER 2

Open your firewall, example port 82

sudo ufw allow 82

Create your 2nd web directory

sudo mkdir -p /var/www/web-folder-name2

Create test content in your web-folder

sudo nano /var/www/web-folder-name2/index.html

and paste any content here to test

Hello World 2!

Create a virtual host file in Nginx

sudo nano /etc/nginx/sites-available/web-folder-name2

and paste the following content

server {
    listen 82;
    server_name _;
    root /var/www/web-folder-name2;
    index index.html;
}

Enable your web server

sudo ln -s /etc/nginx/sites-available/web-folder-name2 /etc/nginx/sites-enabled/

Restart Nginx

sudo systemctl restart nginx

Test in your browser

127.0.0.1:81
127.0.0.1:82

# or
localhost:81
localhost:82

# or if you're on a network
static-ip:81
static-ip:82
theodor
  • 81
  • 1
  • 4
0

Thanks @lotfio.

if server_name is the same cannot be other server blocks using the same server_name, I suppose.

to avoid setting on /etc/hosts we can be do:

on /etc/nginx/sites-available/default:

server {
#...  normal default stuff conf
include /etc/nginx/sites-avilable/localhost_adminer.inc;
incluse /etc/nginx/sites-avilable/localhost_pippo.inc;
#...  
#...  normal default stuff conf
}

if you like to do a reverse proxy on apache2 for adminer like my first try to move from apache2 to nginx you have to configure apache2 to Listen on other port (I choose 8181):

in /etc/nginx/sites-avilable/localhost_adminer.inc

location /adminer/ {
  index conf.php;
  alias /etc/adminer/;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $remote_addr;
  proxy_set_header Host $host;
  proxy_pass http://127.0.0.1:8181/adminer/;
}    

(I'm on ubuntu so adminer package is configured to start on /etc/adminer/)

and so on for pippo, pluto sites etc.

best regards,
Leonardo