2015-10-10 23 views
15

Tôi muốn chạy www.example.comapi.example.com trên cùng một cổng 80.Nginx nhiều khối máy chủ đang nghe cùng một cổng

Đây là những gì tôi có. Tất cả các googles ping của tôi dẫn đến mã dưới đây. Nhưng, điều này không hoạt động.

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; 
     } 
} 

Tôi không biết lý do. Bất kỳ đề xuất về điều này?

Cảm ơn!

+0

Thư mục gốc bị thiếu trong máy chủ ảo 'api.example.com'. – C1sc0

Trả lời

12

Tạo riêng hai tập tin (bạn không cần phải, nhưng nó sẽ được rõ ràng hơn nhiều) trong /etc/nginx/sites-available/www.example.com/etc/nginx/sites-available/api.example.com

File api.example.com của trị nội dung

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; 
     } 
} 

Các www.example nội dung năm của:

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; 
     } 
} 

và cuối cùng cho phép các lĩnh vực: sudo ln -s /etc/nginx/sites-available/www.example.com /etc/nginx/sites-enabled/www.example.comsudo ln -s /etc/nginx/sites-available/api.example.com /etc/nginx/sites-enabled/api.example.com

+0

Tôi không thể hiểu ý bạn là gì "Ngoài ra, hãy thêm máy chủ ảo www.example.com vào tệp cấu hình của bạn:". Bạn có thể giải thích thêm? –

+1

Nếu điều này không làm việc đầu ra của lệnh 'tail -f/var/log/nginx/error.log' là gì nếu bạn cố truy cập trang web? – C1sc0

+0

Cảm ơn bạn rất nhiều. Sắp xếp nó ra. –

Các vấn đề liên quan