KNDheeraj
KNDheeraj

Reputation: 852

How can I host multiple html files present in different locations on nginx server

I have 2 index.html files present in 2 different folders. How can I map my nginx to point to these different folders either by port and location based mapping??

I have tried creating single file in sites-available folder and mapping their directories under location / { but it didn't work

I have:

2 html files at

/var/www/ex1.com/index.html

/var/www/ex2.com/index.html

I would like to do is:

ip:8080 ex1/index.html gets rendered

ip:8081 ex2/index.html gets rendered

And also how can I achieve this

ip/ex1 goes to ex1/index.html

ip/ex2 goes to ex2/index.html

Upvotes: 1

Views: 1851

Answers (1)

Richard Smith
Richard Smith

Reputation: 49672

To configure an Nginx server to listen to a specific port, use the listen directive. See this document for details.

For example:

server {
    listen 8080;
    root /var/www/ex1.com;
}
server {
    listen 8081;
    root /var/www/ex2.com;
}

The URLs http://<ip_address>/ex1 and http://<ip_address>/ex2 will be processed by the same server block, listening on port 80.

You will need to use the alias directive instead of the root directive, as the path to the local file cannot be created by simple concatenation of some value with the URI.

For example:

server {
    listen 80;

    location /ex1 {
        alias /var/www/ex1.com;
    }
    location /ex2 {
        alias /var/www/ex2.com;
    }
}

Note that both the location value and the alias value should have a trailing / or neither have a trailing /. See this document for details.

Upvotes: 1

Related Questions