nginx 监听特定接口

nginx 监听特定接口

我正在运行 Nginx 作为代理服务器,转发到 10.12.96.4。

我希望 NGINX 服务器监听ens3:并通过 tun0 转发请求

这是我的/etc/nginx/sites-available/default

server {
    listen 149.28.239.231:80;
    server_name default_server;

    location / {
       proxy_pass http://45.77.185.160:80;
       # proxy_pass http://10.12.96.4:80;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

server {

    listen 443;
    server_name example.com;

    ssl_certificate           /etc/letsencrypt/live/example.com/cert.pem;
    ssl_certificate_key       /etc/letsencrypt/live/example.com/privkey.pem;

    ssl on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;


    location /
    {
        proxy_pass https://45.77.185.160:443;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

这是我的界面配置

inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 100.68.26.219/10 brd 100.127.255.255 scope global ens3
inet 149.28.239.231/23 brd 149.28.239.255 scope global ens3:1
inet6 2001:19f0:5:65d3:5400:1ff:fea6:9b5/64 scope global
inet6 fe80::5400:1ff:fea6:9b5/64 scope link
inet 10.8.0.3/24 brd 10.8.0.255 scope global tun0

答案1

您可以绑定到相关接口的网络地址。

根据您的接口配置,您的监听配置将如下所示:

listen 149.28.238.0:80;

我正在设置主动/主动 HA 集群,需要它正常工作。我的测试配置如下。

[root@b7311458cb35 nginx_plus]# cat /etc/nginx/sites-enabled/docker-test.local.conf 
# MANAGED BY PUPPET
server {
  listen 172.17.0.0:80;

  server_name           docker-test.local;
  status_zone docker-test;


  index  index.html index.htm index.php;
  access_log            /var/log/nginx/docker-test.local.access.log combined;
  error_log             /var/log/nginx/docker-test.local.error.log;

  location / {
    proxy_pass            http://docker-test;
    proxy_read_timeout    90s;
    proxy_connect_timeout 90s;
    proxy_send_timeout    90s;
    proxy_set_header      Host $host;
    proxy_set_header      X-Real-IP $remote_addr;
    proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header      Proxy "";
  }
}

我的网络详细信息

[root@b7311458cb35 nginx_plus]# ip route
default via 172.17.0.1 dev eth0 
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.4 

测试配置:

[root@b7311458cb35 nginx_plus]# curl  http://172.17.0.4:80/ | head -5
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   612  100   612    0     0   101k      0 --:--:-- --:--:-- --:--:--  119k
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>

答案2

这个问题已经得到解答。无法绑定到接口,因为 nginx 没有 layer2/3 的概念。使用静态 IP,或监控动态 IP 的包装器脚本,根据需要编写新的 .conf 并进行零停机重启。

https://stackoverflow.com/q/39536714/246672

相关内容