V
V
viento2014-10-04 21:15:04
Nginx
viento, 2014-10-04 21:15:04

How to properly set up virtual hosts on nginx?

The task is as follows: you need to configure several services on the local computer (of the "server" type). And they must be on different domains.
The web server is nginx. On my computer I wrote the following in /etc/hosts:

192.168.2.123			cinnamon
192.168.2.123			gitlab
192.168.2.123			redmine

On the "server" exactly the same entries in /etc/hosts. The configs for nginx for the first two domains are as follows:
server {
  listen 80;
  server_name cinnamon www.cinnamon;
  server_tokens off;
  root /home/webservers/public_html/default;

  access_log  /var/log/nginx/default_access.log;
  error_log   /var/log/nginx/default_error.log;

  location / {
    try_files $uri $uri/index.html $uri.html;
  }
}

server {
  listen 80;
  server_name gitlab www.gitlab;
  server_tokens off;
  root /home/webservers/public_html/default1;

  access_log  /var/log/nginx/default_access.log;
  error_log   /var/log/nginx/default_error.log;

  location / {
    try_files $uri $uri/index.html $uri.html;
  }
}

Problem: When accessing cinnamon from my computer, the page for cinnamon opens. When accessing gitlab, the page for cinnamon still opens. It's as if nginx doesn't distinguish which domain the request came from.
What did I do wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
WebSpider, 2014-10-04
@WebSpider

Maybe because both have the same root?root /home/webservers/public_html/default;

A
Alex Chistyakov, 2014-10-04
@alexclear

Try to do nginx -t and make sure that the config contains no errors and that nginx actually applied it. What is your distribution? Are the config sections in the same file?
Make access_log and error_log on different sites into different files, and not into the same one, and look in the logs to which site the calls are made.

V
viento, 2014-10-05
@viento

thanks a lot to everyone for the replies. the error was simple - I forgot to activate the configs :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question