2015-10-10 7 views
15

Я хочу запустить www.example.com и api.example.com на том же порту 80.Nginx несколько серверных блоков, прослушивающих один и тот же порт

Это то, что у меня есть. Все мои пигменты googles приводят к приведенному ниже коду. Но это не работает.

server { 
     listen 80 default_server; 
#  listen [::]:80 default_server ipv6only=on; 

     root /var/www/example.com/html/example/app; 
     index index.html index.htm; 

     # Make site accessible from http://localhost/ 
     server_name www.example.com www.example.org; 

     location/{ 
       # First attempt to serve request as file, then 
       # as directory, then fall back to displaying a 404. 
       try_files $uri $uri/ =404; 
       # Uncomment to enable naxsi on this location 
       # include /etc/nginx/naxsi.rules 
     } 

     location /bower_components { 
       alias /var/www/example.com/html/example/bower_components; 
     } 

     location /scripts { 
       alias /var/www/example.com/html/example/scripts; 
     } 

     location /content { 
       alias /var/www/example.com/html/example/content; 
     } 

     location /api { 
       proxy_set_header Host $host; 
       proxy_set_header X-Real-IP $remote_addr; 
       proxy_pass http://127.0.0.1:3836; 
     } 
} 

server { 
     listen 80 
     server_name api.example.com 

     location/{ 
       proxy_set_header Host $host; 
       proxy_set_header X-Real-IP $remote_addr; 
       proxy_pass http://127.0.0.1:3836; 
     } 
} 

Я не знаю причины. Любые предложения по этому поводу?

Спасибо!

+0

Корневой каталог отсутствует на виртуальном хосте 'api.example.com'. – C1sc0

ответ

12

Создать отдельно два файла (вы не должны, но это будет гораздо понятнее) в /etc/nginx/sites-available/www.example.com и /etc/nginx/sites-available/api.example.com

conten В api.example.com файла

server { 
     listen 80 
     server_name api.example.com 
     root /var/www/api.example.com/html/example/app; #also add a root dir here 
     location/{ 
       proxy_set_header Host $host; 
       proxy_set_header X-Real-IP $remote_addr; 
       proxy_pass http://127.0.0.1:3836; 
     } 
} 

www.example содержание для .com:

server { 
     listen 80 default_server; 
#  listen [::]:80 default_server ipv6only=on; 

     root /var/www/example.com/html/example/app; 
     index index.html index.htm; 

     # Make site accessible from http://localhost/ 
     server_name www.example.com www.example.org; 

     location/{ 
       # First attempt to serve request as file, then 
       # as directory, then fall back to displaying a 404. 
       try_files $uri $uri/ =404; 
       # Uncomment to enable naxsi on this location 
       # include /etc/nginx/naxsi.rules 
     } 

     location /bower_components { 
       alias /var/www/example.com/html/example/bower_components; 
     } 

     location /scripts { 
       alias /var/www/example.com/html/example/scripts; 
     } 

     location /content { 
       alias /var/www/example.com/html/example/content; 
     } 

     location /api { 
       proxy_set_header Host $host; 
       proxy_set_header X-Real-IP $remote_addr; 
       proxy_pass http://127.0.0.1:3836; 
     } 
} 

и, наконец, включить домены: sudo ln -s /etc/nginx/sites-available/www.example.com /etc/nginx/sites-enabled/www.example.com и sudo ln -s /etc/nginx/sites-available/api.example.com /etc/nginx/sites-enabled/api.example.com

+0

Я не могу понять, что вы подразумеваете под «Также добавьте виртуальный хост www.example.com к вашему конфигурационному файлу:». Не могли бы вы объяснить больше? –

+1

Если это не работает, то какой результат команды 'tail -f/var/log/nginx/error.log', если вы пытаетесь добраться до веб-страницы? – C1sc0

+0

Большое вам спасибо. Отсортировано. –

Смежные вопросы