我有一个服务于Web应用程序的Tomcat服务器,我有一个运行在它前面的Nginx服务器作为反向代理。 作为对我之前的问题( Tomcat服务器背后的nginx反向代理 – 如何阻止直接访问服务器? )的答案,我被build议防火墙的Tomcat实例,但根据我从不同论坛的调查结果,限制Tomcat听本地主机似乎是要走的路。 为了防止Tomcat侦听到其他IP,我在连接器configuration中添加了“address = 127.0.0.1”。 整个连接器块是这样的 –
<Connector port="8080" address="127.0.0.1" maxThreads="150" minSpareThreads="25" connectionTimeout="20000" enableLookups="false" maxHttpHeaderSize="8192" protocol="HTTP/1.1" useBodyEncodingForURI="true" redirectPort="8443" acceptCount="100" disableUploadTimeout="true" proxyName=<FQDN> proxyPort="80"/>
在Nginx服务器中,我有这些线路用于服务器configuration。
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; server_name <FQDN>; location / { proxy_pass <FQDN>; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for; } }
现在,如果我尝试使用FQDN访问Web应用程序,Chrome会报告ERR_CONNECTION_REFUSED。 我的Nginxconfiguration似乎是基于我所理解的罪魁祸首。 如何纠正?
为了这个问题的目的,我会假设下面的IP
Nginx的192.168.0.1
Tomcat 192.168.0.2
configurationtomcat服务器
将下面的行address="127.0.0.1"更改为address="192.168.0.2" – 这将告诉Tomcat监听本地IP而不是回环地址。
然后我们要configurationIPtables。 我在这里使用了发电机。
下面的规则是“安全的”,他们不会locking你的远程访问(我已经允许在端口22上的SSH,假设你的SSH端口是默认的),但他们不像他们应该/可能是限制性的。 所以请花一点时间来看看他们。 这也将删除任何现行的规则
#!/bin/sh # iptables script generated 2016-04-20 # http://www.mista.nu/iptables IPT="/sbin/iptables" # Flush old rules, old custom tables $IPT --flush $IPT --delete-chain # Set default policies for all three default chains $IPT -P INPUT DROP $IPT -P FORWARD DROP $IPT -P OUTPUT ACCEPT # Enable free use of loopback interfaces $IPT -A INPUT -i lo -j ACCEPT $IPT -A OUTPUT -o lo -j ACCEPT # All TCP sessions should begin with SYN $IPT -A INPUT -p tcp ! --syn -m state --state NEW -s 0.0.0.0/0 -j DROP # Accept inbound TCP packets $IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT $IPT -A INPUT -p tcp --dport 22 -m state --state NEW -s 0.0.0.0/0 -j ACCEPT # Accepts SSH from everywhere $IPT -A INPUT -p tcp --dport 8080 -m state --state NEW -s 192.168.0.1/32 -j ACCEPT # Accepts connection to port 8080 only from the Nginx Server # Accept inbound ICMP messages $IPT -A INPUT -p ICMP --icmp-type 8 -s 0.0.0.0/0 -j ACCEPT $IPT -A INPUT -p ICMP --icmp-type 11 -s 0.0.0.0/0 -j ACCEPT
configurationNginx服务器
你只需要编辑你的服务器configuration。 我们现在代理到本地IP上的Tomcat服务器。
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; server_name <FQDN>; location / { proxy_pass http://192.168.0.2:8080; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for; } }
您可以代理到一个FQDN,但您需要configuration一个单独的指向Nginx服务器。 例如,如果上面的FQDN是myapp.example.com ,则可以configuration另一个FQDN(如tomcat.example.com并将其parsing为192.168.0.2 ,然后可以使用tomcat.example.comreplace上述nginxconfiguration中的192.168.0.2