![Varnish가 Nginx에 액세스하기 위해 포트 8080을 열 수 없습니다.](https://rvso.com/image/762140/Varnish%EA%B0%80%20Nginx%EC%97%90%20%EC%95%A1%EC%84%B8%EC%8A%A4%ED%95%98%EA%B8%B0%20%EC%9C%84%ED%95%B4%20%ED%8F%AC%ED%8A%B8%208080%EC%9D%84%20%EC%97%B4%20%EC%88%98%20%EC%97%86%EC%8A%B5%EB%8B%88%EB%8B%A4..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는 iptables 쉘 스크립트의 마지막 규칙인 포트 8080입니다. 그러나 텔넷을 시도하면 "연결 거부됨" 오류가 발생합니다.
sridhar@SastraTechnologies:~$ telnet localhost 8080
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
헤더를 가져오기 위해 랩톱에서 컬을 사용하여 바니시가 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을 수신하므로 다른 모든 포트에서는 동일하지 않습니다. 내가 놓친 것이 있나요? Varnish에 액세스할 수 있도록 포트 8080을 얻으려면 어떻게 해야 합니까?
편집하다
시스템 단위 파일 내용
[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 8080;
그러면 Nginx 프로세스가 .NET을 포함하여 해당 서버에서 사용 가능한 모든 네트워크 인터페이스에 바인딩됩니다 localhost
.