nginxとnodeサーバーを使ったアプリで現象が起きています。
IPアドレスは、socket.io の「handshake.address」を使って取得しようとしています。
nginxでリバースプロキシを使うとヘッダー情報のIPアドレスが127.0.0.1に書き換わる(?)という事象があるというのは
サイトでいくつか見て、それぞれ対処法を試してみたのですが、変化ありません。。
おかしなところがありましたら、ご教授頂きたいです。
よろしくお願い致します。
- サブドメイン(仮):hoge.huga.jp
- NodeのHttpサーバー:http://0.0.0.0:2222
- Nodeのwsサーバー:http://0.0.0.0:1111
設定ファイル
nginx.conf
conf
1user nginx; 2worker_processes 1; 3 4error_log /var/log/nginx/error.log warn; 5pid /var/run/nginx.pid; 6 7 8events { 9 worker_connections 1024; 10} 11 12 13http { 14 include /etc/nginx/mime.types; 15 default_type application/octet-stream; 16 17 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 18 '$status $body_bytes_sent "$http_referer" ' 19 '"$http_user_agent" "$http_x_forwarded_for"'; 20 21 access_log /var/log/nginx/access.log main; 22 23 sendfile on; 24 #tcp_nopush on; 25 26 keepalive_timeout 65; 27 28 # ! modified ! # 29 gzip on; 30 proxy_buffering on; 31 proxy_buffer_size 8k; 32 proxy_buffers 100 8k; 33 34 server_tokens off; 35 36 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 37 proxy_set_header Client-IP $remote_add; 38 proxy_set_header Host $http_host; 39 proxy_redirect off; 40 41 include /etc/nginx/conf.d/*.conf; 42}
default.conf
conf
1map $http_upgrade $connection_upgrade { 2 default upgrade; 3 '' close; 4} 5 6server { 7 # demo site(http://demo.localhost/) 8 listen 80; 9 server_name demo.localhost; 10 root /var/www/hoge/demo_site; 11 index index.html; 12} 13 14server { 15 listen 80; 16 server_name hoge.huga.jp; 17 18 location / { 19 proxy_set_header Host $http_host; 20 proxy_pass http://0.0.0.0:2222; 21 } 22 23 location /socket.io/ { 24 proxy_pass http://0.0.0.0:1111; 25 proxy_http_version 1.1; 26 proxy_set_header Upgrade $http_upgrade; 27 proxy_set_header Connection $connection_upgrade; 28 } 29 30 location /socket/ { 31 proxy_pass http://0.0.0.0:1111/; 32 proxy_http_version 1.1; 33 proxy_set_header Upgrade $http_upgrade; 34 proxy_set_header Connection $connection_upgrade; 35 } 36 37} 38 39server { 40 listen 443; 41 server_name hoge.huga.jp; 42 43 location / { 44 proxy_pass http://0.0.0.0:2222; 45 proxy_http_version 1.1; 46 proxy_set_header Upgrade $http_upgrade; 47 proxy_set_header Connection $connection_upgrade; 48 } 49} 50 51server { 52 listen 80 default_server; 53 server_name _; 54 return 444; 55}
socket_ctrl.js
javascript
1 ~ 省略 ~ 2 3 //接続確立時の処理 4 connect = io.sockets.on('connection', function (socket) { 5 6 // 接続時 7 socket.on('connected', function (r) { 8console.log(socket.handshake); // ひとまずここでヘッダー情報をとれるか確認してます。
(※原因はnginxの設定ファイルだと今のところ思っているので、websocketはタグ付けしません。)
解決方法
結局、serverのレベルに「X-Forwarded-For」の設定を入れていたので効いていなかったようです。
以下のように location のレベルに指定をいれて動くようになりました。
※nginx.confの「proxy_set_header」は全て削除しました。
conf
1server { 2 listen 80; 3 server_name hoge.huga.jp; 4 5 location / { 6 proxy_pass http://0.0.0.0:2222; 7 } 8 9 location /socket.io/ { 10 proxy_pass http://0.0.0.0:1111; 11 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 12 proxy_http_version 1.1; 13 proxy_set_header Upgrade $http_upgrade; 14 proxy_set_header Connection $connection_upgrade; 15 } 16 17 location /socket/ { 18 proxy_pass http://0.0.0.0:1111/; 19 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 20 proxy_http_version 1.1; 21 proxy_set_header Upgrade $http_upgrade; 22 proxy_set_header Connection $connection_upgrade; 23 } 24 25}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/09 11:50
2015/12/09 12:03
2015/12/09 12:46
2015/12/10 01:08
2015/12/10 01:53
2015/12/10 05:47
2015/12/11 05:58
2015/12/11 05:59
2015/12/14 06:44