nginx内网证书自签及其配置

仅限内网,公网不安全

服务器生成证书,替换下面的IP

找个文件夹,比如/home/cert ,这样执行下面的命令后值直接就会在当前目录生成文件

文件 用途
ca.crt 根证书,访问的电脑 / 浏览器需要导入这个信任,否则报不安全,这个拷出来,用户需要就给他
ca.key CA 根私钥,保存好,不要给 nginx
server.crt nginx 证书公钥
server.key nginx 证书私钥

nginx的配置在最下面

替换内网IP,直接执行

进入目录 /home/cert


# ----------------------1.生成根CA(只需要生成一次)----------------------
openssl genrsa -out ca.key 4096

openssl req -new -key ca.key -subj "/C=CN/ST=Fujian/L=Xiamen/O=InnerTest/OU=Dev/CN=InnerTest‑RootCA" -out ca.csr

# 根证书有效期:99年 = 36135天
openssl x509 -req -days 36135 -sha256 -in ca.csr -signkey ca.key -out ca.crt -extfile <(
cat <<EOF
[ v3_ca ]
basicConstraints = critical,CA:TRUE
keyUsage = critical, digitalSignature, keyCertSign, cRLSign
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
EOF
)

# ----------------------2.生成nginx服务器证书(IP:10.97.141.235,99年有效期)----------------------
openssl genrsa -out server.key 2048

openssl req -new -key server.key -subj "/C=CN/ST=Fujian/L=Xiamen/O=InnerTest/OU=Nginx/CN=10.97.141.235" -out server.csr

# 服务证书同样99年,SAN包含你的内网IP
openssl x509 -req -days 36135 -sha256 \
-in server.csr \
-CA ca.crt -CAkey ca.key \
-CAcreateserial \
-out server.crt \
-extfile <(cat <<EOF
[v3_ext]
basicConstraints = CA:FALSE
keyUsage = critical,digitalSignature,keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
IP.1 = 10.97.141.235
IP.2 = 127.0.0.1
EOF
)

查看当前模块是否存在 nginx的ssl配置 --with-http_ssl_module

./nginx -V

如果存在该配置,那直接修改nginx的配置文件,配置nginx证书,重启nginx就好了


不存在 --with-http_ssl_module 按照下面步骤重新编译nginx

先确认当前编译参数

./nginx -V

进入 nginx 源码目录,重新编译(重点!!)

复制上面 nginx -V 打印出来的 configure arguments,在末尾追加 --with-http_ssl_module

示例(你要替换成自己原来的参数!)

./configure --prefix=/usr/local/nginx 【这里粘贴你原来所有参数】 --with-http_ssl_module

只编译,千万不要 make install!!

make

替换 nginx 二进制文件

备份旧nginx

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

将新编译好的nginx程序覆盖过去

cp objs/nginx /usr/local/nginx/sbin/nginx

验证模块,测试配置

检查模块是否存在

/usr/local/nginx/sbin/nginx -V | grep http_ssl_module

测试配置是否正常

/usr/local/nginx/sbin/nginx -t

修改nginx配置,重启nginx

nginx配置


server {
	
	listen       8082 ssl;
       listen   [::]:8082 ssl;
       server_name  10.97.141.235;
       client_max_body_size 1024m;
       ssl_certificate      /home/cert/server.crt;
       ssl_certificate_key  /home/cert/server.key;

       ssl_session_cache    shared:SSL:1m;
       ssl_session_timeout  5m;
       ssl_protocols TLSv1.2 TLSv1.3;

       ssl_ciphers HIGH:!aNULL:!MD5;
       #是否由服务器决定采用哪种加密算法
       ssl_prefer_server_ciphers on;

       #开启htst功能
       add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload" always;
       #读取请求头的超时时间,若超过所设定的大小,返回408错误。默认60s
       client_header_timeout 10s;
       #读取请求实体的超时时间,若超过所设定的大小,返回413错误。默认60s
       client_body_timeout 60s;

       #http请求无法立即被容器(tomcat, netty等)处理,被放在nginx的待处理池中等待被处理。此参数为等待的最长时间,默认为60秒,官方推荐最长不要超过75秒
       proxy_connect_timeout 5s;
       #http请求被容器(tomcat, netty等)处理后,nginx会等待处理结果,也就是容器返回的response。此参数即为服务器响应时间,默认60秒。
       proxy_read_timeout 60s;
       #http请求被服务器处理完后,把数据传返回给Nginx的用时,默认60秒。
       proxy_send_timeout 180s;
                                                                                
	add_header X-Frame-Options "SAMEORIGIN";
	server_tokens off;
	

	root /home/front/nw/;
	
	index index.html index.htm;
	
	location /brt-labor-nw {
	
		proxy_pass http://127.0.0.1:8882;
		
		proxy_set_header Host 10.97.141.235;
           	proxy_set_header  X-Real-IP        $remote_addr;
            	proxy_set_header X-NginX-Proxy true;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection $connection_upgrade;
                # 这里配置单个代理跨域,跨域配置
		add_header 'Access-Control-Allow-Origin' *;	
		add_header 'Access-Control-Allow-Credentials' 'true';
		add_header 'Access-Control-Allow-Methods' *;
		add_header 'Access-Control-Allow-Headers' *;
		proxy_ignore_client_abort on;

		limit_except GET POST PUT DELETE OPTIONS {
        		allow all;
    		}
	
	}


	location / {
    		try_files $uri $uri/ /index.html;
	}
	

}

posted @ 2026-09-15 16:52  窃窃私语QAQ  阅读(8)  评论(0)    收藏  举报