TLDR:我正在尝试将Nginx配置为托管网站并同时充当反向代理。该网站位于“ www.example.com:443”上,反向代理位于“ vpn.example.com:443”上的另一台OpenVPN服务器上。我想我知道答案是“ Nginx无法配置为在单个端口上侦听两种不同类型的流量”(Web流量和反向代理流量),但是我想请问那些知识渊博的人。
现在,转到更长的版本。我确实有一个类似这样的工作设置:
+-----------------------+
| |
| Nginx |
| (Rev Proxy Only) |
| |
| 192.168.0.100 |
| vpn.example.com:443 |
| www.example.com:443 |
| |
+-----------------------+
/ \
/ \
+-----------------------+ +-----------------------+
| | | |
| OpenVPN | | LAMP Stack |
| (VPN) | | (Website) |
| | | |
| 192.168.0.101 | | 192.168.0.102 |
| vpn.example.com:443 | | www.example.com:443 |
| | | |
+-----------------------+ +-----------------------+
这里有几点注意事项。 Nginx(v1.14.2)服务器是CentOS v7.6。它使用“ --with-stream”和“ --with-stream_ssl_preread_module”进行配置。它还使用/etc/nginx/nginx.conf中的所有默认值,除了它包括带有proxy_pass的stream {}块。 conf文件看起来像这样:
/ etc / nginx / nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
include /etc/nginx/proxy.conf;
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
/ etc / nginx / proxy.conf
stream {
map $ssl_preread_server_name $name {
www.example.com lamp;
vpn.example openvpn;
default https_default_backend;
}
server {
listen 192.168.0.100:443;
proxy_pass $name;
ssl_preread on;
}
upstream lamp {
server 192.168.0.102:443;
}
upstream openvpn {
server 192.168.0.101:443;
}
upstream https_default_backend {
server 192.168.0.101:443;
}
log_format basic '[$time_local] $remote_addr - $protocol '
'STATUS: $status BYTES_SENT: $bytes_sent '
'BYTES_RECEIVED: $bytes_received '
'SENT_TO: $upstream_addr MSECS: $msec '
'SESSION_TIME: $session_time '
'UPSTREAM_BYTES_SENT: $upstream_bytes_sent '
'UPSTREAM_BYTES_RECEIVED: $upstream_bytes_received '
'UPSTREAM_CONNECTION_TIME: $upstream_connect_time"';
access_log /var/log/nginx/stream.mfgs.dev_access.log basic;
error_log /var/log/nginx/stream.mfgs.dev_error.log;
}
再次,这是我当前的配置,这里的一切正常。网站流量流式传输到Web服务器,VPN流量流式传输到VPN服务器。所有这些工作。
我的目标是将LAMP堆栈(Linux,Apache,MySQL,PHP)更改为LEMP堆栈(Linux,Nginx,MySQL,PHP),并将Nginx反向代理和网站合并到同一服务器上。任何专门定向到“ www.example.com:443”的流量都将进入Nginx网站,其他所有内容都将传递到VPN服务器,如下所示:
+-----------------------+
| |
| Nginx |
| (Website & Rev Proxy) |
| |
| 192.168.0.100 |
| vpn.example.com:443 |
| www.example.com:443 |
| |
+-----------------------+
|
|
+-----------------------+
| |
| OpenVPN |
| (VPN) |
| |
| 192.168.0.101 |
| vpn.example.com:443 |
| |
+-----------------------+
我已经尝试在单独的服务器(192.168.0.105)上安装Nginx。我在该服务器上启动并运行了网站。但是,当我包含proxy.conf时,它使用'nginx -t'可以正确测试,但是Nginx服务无法在此服务器上重新启动。失败消息是:
[nginx:[emerg] bind()到192.168.0.105:443失败(98:地址已在使用中)
我相信这是因为http和stream块都在同一IP和端口上侦听。是否可以通过某种方式将Nginx配置为充当特定FQDN的Web服务器:端口和代理(通过流传输)将其他所有内容传输到另一台服务器?
谢谢你。
address already in use
服务时将遇到nginx
错误。这是由于nginx
stream和
http服务都配置为侦听端口:443:
http {
server {
listen 192.168.0.105:443;
server_name www.example.com;
}
}
stream {
server {
listen 192.168.0.105:443;
proxy_pass $name;
ssl_preread on;
}
}
与http
服务不同,该服务允许基于命名的服务器使用相同的端口。 Nginx将http
和stream
都视为不同的服务。我有2种解决方案可以解决同一节点中的主机:让stream
和http
。解决方案1
www.example.com
收听127.0.0.1:443
由于大多数节点默认情况下应具有回送接口。我相信您想直接公开stream:443
服务而不是www.example.com:443
。让www.example.com
听127.0.0.1:443
对于解决address already in use
问题来说不是一个坏主意。
http {
server {
listen 127.0.0.1:443;
server_name www.example.com;
}
}
解决方案2:让
www.example.com
监听Unix套接字类似于解决方案1,但对www.example.com
使用unix套接字,以避免额外的TCP开销,从而获得一点性能提升。
http {
server {
listen unix:/var/run/nginx.sock;
server_name www.example.com;
}
}
对于这两种解决方案,请记住使用upstream
或unix套接字重新配置stream
部分中的127.0.0.1
。
我希望可以找到更多替代方法,但这是我到目前为止可以想到的。