Nginx同时监听IPV6+IPV4,实现正向和反向,配置双栈虚拟域名

实用教程 admin 3个月前 (03-03) 370次浏览 0个评论

同时监听IPV6+IPV4
nginx.conf 配置如下

user  root;
worker_processes  8;
#error_log  /opt/server/department/nginx/logs/error.log warn;
error_log  /opt/server/department/nginx/logs/error.log info;
pid        /var/run/nginx.pid;


events {
    worker_connections  2048;
}


http {
    include       /opt/server/department/nginx/conf/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  /opt/server/department/nginx/logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    #X-Forwarded-For:简称XFF头,它代表客户端---HTTP的请求端真实的IP
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #隐藏NGINX版本信息
    server_tokens off;
    #gzip  on;
	
	#将IPV4+IPV6的8057端口请求反向代理至tomcat8080接口地址
	server{
      listen       8057;
      listen    [::]:8057 ipv6only=on;
      server_name  tomcat_server;
      location /interfaceUri {
	#proxy_pass http://[fe80::5154:ff:fabc:d8ae]:8080/northbound/query;        
	proxy_pass http://101.96.15.40:8080/northbound/query;
      }
  		 }	
    
    #IPV4+IPV6监听8077端口 定期查询html页面生成日志保证cache服务器心跳
    server{
      listen       8077;
      listen       [::]:8077 ipv6only=on;
      server_name X-Cache;
      location / {
        root /opt/server/department/nginx/html;
      }
    }
    include /opt/server/department/nginx/conf/conf.d/*.conf;
}

同时监听IPV4+IPV6的8057端口
listen 8057;
listen [::]:8057 ipv6only=on;
这样配置可以同时监听IPV4+IPV6,测试过没问题

同时监听IPV4+IPV6的8057端口
listen [::]:8057;
这样配置可能存在ipv4不通的情况,暂时没找到原因

双栈虚拟域名配置示例

server {
    listen       30050;
    listen       [::]:30050 ipv6only=on;
    server_name  www.test.cn;

    access_log  /var/log/nginx/access-30050-www.log;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/www/html;
    }

server {
    listen       30050;
    listen       [::]:30050;
    server_name  dev.test.cn;

    access_log  /var/log/nginx/access-30050-dev.log;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/dev/html;
    }
    
server {
    listen       30050;
    listen       [::]:30050;
    server_name  ops.test.cn;

    access_log  /var/log/nginx/access-30050-ops.log;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/ops/html;
    }

}

ipv6only=on只能添加一次,否则nginx会报错端口重复
如上配置即可支持同一Nginx下单端口3个域名

————————————————
版权声明:本文为CSDN博主「skywin88」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Skywin88/article/details/84747066


VPS小白 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址