nginx配置负载均衡添加ssl,校验配置报错

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


}

stream {
  upstream emqx_cluster {
  	  # nginx 的三个实例
      server 192.168.0.33:1883 max_fails=2 fail_timeout=30s;
      server 192.168.0.34:1883 max_fails=2 fail_timeout=30s;
  }

  server {
      listen 1883;
      proxy_pass emqx_cluster ;
      proxy_buffer_size 4k;
      proxy_connect_timeout 10;
  }
  
  upstream stream_backend {
      zone tcp_servers 64k;
      hash $remote_addr;
      server 192.168.0.33:1883 max_fails=2 fail_timeout=30s;
      server 192.168.0.34:1883 max_fails=2 fail_timeout=30s;
  }

  server {
      listen 8883;
      proxy_pass stream_backend;
      ssl_certificate /home/hehuang/nginx/nginx/certs/emqx.pem;
	  ssl_certificate_key /home/hehuang/nginx/nginx/certs/emqx.key;
	  ssl_client_certificate /home/hehuang/nginx/nginx/certs/ca.pem;
	  ssl_verify_client on;
	  ssl_verify_depth 1;
  }
  
	upstream mqtt_server_manage {
      server 192.168.0.33:18083 max_fails=2 fail_timeout=30s;
      server 192.168.0.34:18083 max_fails=2 fail_timeout=30s;
  }
  server {
	  listen 18083;
      proxy_pass mqtt_server_manage;
      proxy_buffer_size 4k;
      proxy_connect_timeout 1;
      #proxy_send_timeout 1;
      #proxy_read_timeout 1;
  }  
  
}

nginx校验配置文件时报错:nginx: [emerg] “ssl_certificate” directive is not allowed here in /home/hehuang/nginx/nginx/conf/nginx.conf:143

GPT:

你遇到的错误是由于在 stream 模块中使用了 ssl_certificate 指令,而 stream 模块不支持该指令。在 Nginx 中,ssl_certificate 和其他 SSL 相关的配置一般用于 HTTP 模块,而不是 stream 模块。

如果你需要在 stream 模块中启用 SSL,可以使用 sslssl_certificate 相关的指令,但它们的配置语法略有不同。可以修改你的 stream 配置,具体方法如下:

stream {
    upstream emqx_cluster {
        server 192.168.0.33:1883 max_fails=2 fail_timeout=30s;
        server 192.168.0.34:1883 max_fails=2 fail_timeout=30s;
    }

    server {
        listen 1883;
        proxy_pass emqx_cluster;
        proxy_buffer_size 4k;
        proxy_connect_timeout 10;
    }

    upstream stream_backend {
        zone tcp_servers 64k;
        hash $remote_addr;
        server 192.168.0.33:1883 max_fails=2 fail_timeout=30s;
        server 192.168.0.34:1883 max_fails=2 fail_timeout=30s;
    }

    server {
        listen 8883 ssl;  # 在 stream 模块中启用 SSL
        proxy_pass stream_backend;
        
        # SSL 配置
        ssl_certificate /home/hehuang/nginx/nginx/certs/emqx.pem;
        ssl_certificate_key /home/hehuang/nginx/nginx/certs/emqx.key;
        ssl_client_certificate /home/hehuang/nginx/nginx/certs/ca.pem;
        ssl_verify_client on;
        ssl_verify_depth 1;
    }

    upstream mqtt_server_manage {
        server 192.168.0.33:18083 max_fails=2 fail_timeout=30s;
        server 192.168.0.34:18083 max_fails=2 fail_timeout=30s;
    }

    server {
        listen 18083;
        proxy_pass mqtt_server_manage;
        proxy_buffer_size 4k;
        proxy_connect_timeout 1;
        #proxy_send_timeout 1;
        #proxy_read_timeout 1;
    }
}

关键修改点:

  1. listen 8883 后面添加了 ssl,启用了 SSL。
  2. 保留了 SSL 证书和相关配置,但放在 server 块中,并且在 stream 中语法正确。

修改后再试试重新加载配置。