2015-10-10 12 views
15

同じポート80www.example.comapi.example.comを実行します。Nginx複数のサーバーブロックが同じポートをリッスンしています

これは私が持っているものです。私のすべてのgoogles pingは以下のコードにつながります。しかし、これは動作していません。

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

は別に2つの/etc/nginx/sites-available/www.example.com内のファイル(あなたが持っていないが、それがより明確になります)と/etc/nginx/sites-available/api.example.com

api.example.comファイルのconten

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とを210

+0

私はあなたが "www.example.com仮想ホストを設定ファイルに追加する"という意味を理解できません: "。もっと説明できますか? –

+1

ウェブページにアクセスしようとすると、 'tail -f/var/log/nginx/error.log'コマンドの出力はどうなりますか? – C1sc0

+0

ありがとうございました。それを並べ替えました。 –

関連する問題