![Varnish が Nginx にアクセスするためにポート 8080 を開けません](https://rvso.com/image/762140/Varnish%20%E3%81%8C%20Nginx%20%E3%81%AB%E3%82%A2%E3%82%AF%E3%82%BB%E3%82%B9%E3%81%99%E3%82%8B%E3%81%9F%E3%82%81%E3%81%AB%E3%83%9D%E3%83%BC%E3%83%88%208080%20%E3%82%92%E9%96%8B%E3%81%91%E3%81%BE%E3%81%9B%E3%82%93.png)
私は、Drupal ウェブサイトの HTTP および HTTPS リクエストを受信するために Varnish と Hitch を設定しています。Drupal ウェブサイトは、Digital Ocean VPS 上の Nginx で実行され、ポート 80 と 443 でリッスンしていました。私は、すべてのサーバー ブロックをポート 8080 でリッスンするように変更しました。私は、同じマシンで 80 でリッスンするように Varnish を設定しました。私は、iptables を設定するためのシェル スクリプトを作成しました。スクリプトには、次の行が含まれています。
iptables -F
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 9999 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 25 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 465 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 110 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 995 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 143 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 993 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 4949 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT
iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
ipchains -A input -p icmp --icmp-type timestamp-request -j DROP ipchains -A output -p icmp --icmp-type timestamp-reply -j DROP
iptables -P OUTPUT ACCEPT
iptables -P INPUT DROP
iptables -L -n
iptables-save | sudo tee /etc/sysconfig/iptables
service iptables restart
上記のスクリプトを実行した後、iptablesルールを一覧表示すると、出力は次のようになります。
sridhar@SastraTechnologies:~$ sudo iptables -L
[sudo] password for sridhar:
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
DROP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,PSH,ACK,URG/NONE
DROP tcp -- anywhere anywhere tcp flags:!FIN,SYN,RST,ACK/SYN state NEW
DROP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,PSH,ACK,URG/FIN,SYN,RST,PSH,ACK,URG
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere tcp dpt:http
ACCEPT tcp -- anywhere anywhere tcp dpt:https
ACCEPT tcp -- anywhere anywhere tcp dpt:9999
ACCEPT tcp -- anywhere anywhere tcp dpt:smtp
ACCEPT tcp -- anywhere anywhere tcp dpt:urd
ACCEPT tcp -- anywhere anywhere tcp dpt:pop3
ACCEPT tcp -- anywhere anywhere tcp dpt:pop3s
ACCEPT tcp -- anywhere anywhere tcp dpt:imap2
ACCEPT tcp -- anywhere anywhere tcp dpt:imaps
ACCEPT tcp -- anywhere anywhere tcp dpt:munin
ACCEPT tcp -- anywhere anywhere tcp dpt:http-alt
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain DOCKER (0 references)
target prot opt source destination
Chain DOCKER-ISOLATION (0 references)
target prot opt source destination
http-alt はポート 8080 です。これは、iptables シェル スクリプトの最後のルールです。ただし、telnet を試みると、「接続が拒否されました」というエラーが表示されます。
sridhar@SastraTechnologies:~$ telnet localhost 8080
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
私のラップトップから curl を使用してヘッダーを取得し、varnish が Nginx に接続できるかどうかを確認してみました。これが出力です (明らかな理由により、IP アドレスは難読化されています)
sridhar@sridhar-HP-Laptop-15-bs0xx:~$ curl -I 139.99.99.99
HTTP/1.1 503 Backend fetch failed
Date: Mon, 11 Jan 2021 08:57:53 GMT
Server: Varnish
Content-Type: text/html; charset=utf-8
Retry-After: 5
X-Varnish: 4271
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive
しかし、netstatを使用してポートの状態を確認すると、Nginxがポート8080でリッスンしていることがわかります。
sridhar@SastraTechnologies:~$ sudo netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:6082 0.0.0.0:* LISTEN 2246/varnishd
tcp 0 0 127.0.0.1:8999 0.0.0.0:* LISTEN 1543/perl
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 541/php-fpm.conf)
tcp 0 0 139.99.99.99:3306 0.0.0.0:* LISTEN 1472/mysqld
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 9050/sshd
tcp 0 0 139.99.99.99:8080 0.0.0.0:* LISTEN 4071/nginx -g daemo
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2246/varnishd
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 4071/nginx -g daemo
tcp6 0 0 :::9999 :::* LISTEN 9050/sshd
tcp6 0 0 :::80 :::* LISTEN 2246/varnishd
tcp6 0 0 :::4949 :::* LISTEN 7923/perl
しかし、すべての IP ではなくパブリック IP のみのポート 80 をリッスンしており、他のすべてのポートで同じではありません。何か見落としている点はありますか? ポート 8080 を Varnish からアクセス可能にするにはどうすればよいですか?
編集
システムユニットファイルの内容
[Unit]
Description=Varnish HTTP accelerator
Documentation=https://www.varnish-cache.org/docs/4.1/ man:varnishd
[Service]
Type=simple
LimitNOFILE=131072
LimitMEMLOCK=82000
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
ExecReload=/usr/share/varnish/reload-vcl
ProtectSystem=full
ProtectHome=true
PrivateTmp=true
PrivateDevices=true
[Install]
WantedBy=multi-user.target
Varnish のバックエンド定義
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
}
sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.
}
sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
}
今のところバックエンドのデフォルト設定のみ変更しました
Nginx の設定
server {
listen mydomain.co.in:8080;
server_name mydomain.co.in www.mydomain.co.in;
root /home/sridhar/public_html/newanybank/public; ## <-- Your only path reference.
keepalive_timeout 70;
access_log /home/sridhar/public_html/newanybank/log/access.log;
error_log /home/sridhar/public_html/newanybank/log/error.log;
# Enable compression, this will help if you have for instance advagg ^ modue# by serving Gzip versions of the files.
gzip_static on;
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Very rarely should these ever be accessed outside of your lan
location ~* \.(txt|log)$ {
allow 192.168.0.0/16;
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
# No no for private
location ~ ^/sites/.*/private/ {
return 403;
}
# Block access to "hidden" files and directories whose names begin with a
# period. This includes directories used by version control systems such
# as Subversion or Git to store control files.
location ~ (^|/)\. {
return 403;
}
location / {
try_files $uri $uri/ @rewrite;
expires max;
}
location @rewrite {
# You have 2 options here
# For D7 and above:
# Clean URLs are handled in drupal_environment_initialize().
rewrite ^ /index.php;
# For Drupal 6 and bwlow:
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
#rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
#fastcgi_pass unix:/tmp/phpfpm.sock;
fastcgi_pass 127.0.0.1:9000;
}
# Fighting with Styles? This little gem is amazing.
# This is for D6
#location ~ ^/sites/.*/files/imagecache/ {
# This is for D7 and D8
location ~ ^/sites/.*/files/styles/ {
try_files $uri $uri/ @rewrite;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 365d;
log_not_found off;
add_header Cache-Control "public, no-transform";
}
location ~* \.(pdf|css|html|js|swf)$ {
expires 7d;
log_not_found off;
add_header Cache-Control "public, no-transform";
}
答え1
おそらく、Nginx 設定のディレクティブlisten mydomain.co.in:8080;
が問題です。
Nginx は、ドメインによって解決された IP のネットワーク インターフェイスのみをリッスンします。ドメインはプライバシー保護のため編集されていることは承知しています。しかし、おそらく私の言っている意味はおわかりでしょう。
139.99.99.99:8080
これは出力のローカル アドレスにも反映されますnetstat
。
アドレスlocalhost
はその IP アドレスに解決されず、ループバック インターフェイスを使用します。これがおそらく機能しない理由です。
Nginx の listen アドレスを次のように変更してください。
listen 8080;
これにより、Nginx プロセスが、 を含むそのサーバー上の利用可能なすべてのネットワーク インターフェイスにバインドされますlocalhost
。