当前位置:   article > 正文

Nginx配置HTTPS_nginx configure ssl

nginx configure ssl

前置条件:

1、服务器已经安装nginx并且可以通过http正常访问
2、拥有ssl证书,没有的可以去阿里购买或者免费申请一年
  • 1
  • 2

一、nginx 的ssl模块安装

查看 nginx 是否安装http_ssl_module 模块。

$ /usr/local/nginx/sbin/nginx -V
  • 1

在这里插入图片描述
如果出现 configure arguments: --with-http_ssl_module,则已安装(下面的步骤可以直接跳过,进入nginx.conf 配置)

下载 nginx安装包,nginx 官网 1.24.0 稳定版本tar.gz包。

# 下载安装包到 src 目录
$ cd /usr/local/src
$ wget http://nginx.org/download/nginx-1.24.0.tar.gz
  • 1
  • 2
  • 3

解压安装包

$ tar -zxvf nginx-1.24.0.tar.gz
  • 1

配置ssl模块

$ cd nginx-1.24.0
$ ./configure --prefix=/usr/local/nginx --with-http_ssl_modul
  • 1
  • 2

使用 make 命令编译(使用 make install会重新安装nginx),此时当前目录会出现 objs 文件夹。用新的 nginx 文件覆盖当前的 nginx 文件。

$ cp ./objs/nginx /usr/local/nginx/sbin/
  • 1

再次查看安装的模块 (configure arguments: --with-http_ssl_module说明ssl模块已安装)。

$ /usr/local/nginx/sbin/nginx -V
  • 1

nginx version:nginx/1.24.0
……
configure arguments:with-http_ssl_module

二、生成证书文件

证书存放目录:/usr/local/nginx/sslfiles

1、直接生成的方式

# 使用OpenSSL 命令生成证书文件
mkdir /usr/local/nginx/sslfiles
cd /usr/local/nginx/sslfiles
# 生成文件,使用自定义域名,密码根据需要修改;这里不能用IP地址,否则应用程序会警告
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -sha256 -keyout HostDomain.key -out HostDomain.cer -subj "/CN=gov.bizpro.cn"
  • 1
  • 2
  • 3
  • 4
  • 5

参数说明:

  • -days:证书的有效时长,单位是day

  • -x509:说明生成自签名证书

  • -nodes:如果指定 -newkey 自动生成秘钥,那么 -nodes 选项说明生成的秘钥不需要加密,即不需要输入 passphase

  • -key:指定已有的秘钥文件生成秘钥请求,只与生成证书请求选项-new配合

  • -newkey:-newkey 是与 -key 互斥的,-newkey 是指在生成证书请求或者自签名证书的时候自动生成密钥,然后生成的密钥名称由 -keyout 参数指定。当指定 newkey 选项时,后面指定 rsa:bits 说明产生 rsa 密钥,位数由 bits 指定。 如果没有指定选项 -key 和 -newkey,默认自动生成秘钥

  • rsa:2048:rsa表示创建rsa私钥,2048表示私钥的长度。

  • -keyout:指定私钥保存位置。

  • -out:新的证书请求文件位置。

  • -config:指定req的配置文件,指定后将忽略所有的其他配置文件。如果不指定则默认使用 /etc/pki/tls/openssl.cnf 中req段落的值

2、使用配置文件生成方式

创建配置文件 req.cnf


# 定义输入用户信息选项的"特征名称"字段名,该扩展字段定义了多项用户信息。
distinguished_name = req_distinguished_name

# 生成自签名证书时要使用的证书扩展项字段名,该扩展字段定义了要加入到证书中的一系列扩展项。
x509_extensions = v3_req

# 如果设为no,那么 req 指令将直接从配置文件中读取证书字段的信息,而不提示用户输入。
prompt = no

[req_distinguished_name]
#国家代码,一般都是CN(大写)
C = CN
#省份
ST = gd
#城市
L = gz
#企业/单位名称, 组织 (O)
O = bizpro
#企业部门, 组织单位 (OU)	
#OU = bizpro
#证书的主域名, 公用名 (CN)
CN = gov.bizpro.cn

##### 要加入到证书请求中的一系列扩展项 #####
[v3_req]
keyUsage = critical, digitalSignature, keyAgreement
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[ alt_names ]
IP.1 = 192.168.1.102
# IP.2 = 192.168.1.105

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

在放置该文件的目录下执行以下命令

openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout server.key -out server.crt -config req.cnf -sha256
  • 1

三、修改 nginx.conf 配置文件

找到 nginx.conf,在对应的配置文件加上两处配置

# 1、端口后端增加ssl配置,表示该端口是https协议
listen 8443 ssl;
# 2、在server中配置证书地址, 生成 cer 和 crt 证书文件都可以
ssl_certificate /home/java/nginx/sslfiles/server.crt;
ssl_certificate_key /home/java/nginx/sslfiles/server.key;
#ssl_certificate /home/java/nginx/sslfiles/HostDomain.cer;
#ssl_certificate_key /home/java/nginx/sslfiles/HostDomain.key;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

重载nginx。

四、示例说明(8080为http协议;8443为https协议)

server {
	#按实际情况修改; 监听端口 建议80
	listen 8080;
	#按实际情况修改; nginx服务器(本机)地址或域名
	server_name 192.168.11.55;
	#请求体内容最大 大小
	client_max_body_size 20m;
	#请求体缓存 大小
	client_body_buffer_size 3m;
	location /license-admin {
		proxy_pass http://admin-hosts;
		proxy_set_header Host $host:$server_port;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_connect_timeout 800s;
		proxy_send_timeout 800s;
		proxy_read_timeout 800s;
		error_log /usr/local/nginx/logs/admin_error.log warn;
		access_log /usr/local/nginx/logs/admin_access.log main;
	}
}
 
 
server {
	#按实际情况修改; 监听端口 建议443
	listen 8443 ssl;
	#按实际情况修改; nginx服务器(本机)地址或域名
	server_name 192.168.11.55;
	ssl_certificate /home/java/nginx/sslfiles/server.crt;
	ssl_certificate_key /home/java/nginx/sslfiles/server.key;
	#请求体内容最大 大小
	client_max_body_size 20m;
	#请求体缓存 大小
	client_body_buffer_size 3m;
	 
	 
	 
	#https代理http服务(适用tomcat未配https)
	location /license-admin/ {
		proxy_pass http://admin-hosts;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header Host $http_host;
		#https代理http(特性配置)
		proxy_redirect http:// $scheme://;
		#https代理http(特性配置)
		port_in_redirect on;
		#https代理http(特性配置)
		proxy_set_header X-Forwarded-Proto http;
		proxy_connect_timeout 800s;
		proxy_send_timeout 800s;
		proxy_read_timeout 800s;
		error_log /usr/local/nginx/logs/admin_ssl_error.log warn;
		access_log /usr/local/nginx/logs/admin_ssl_ccess.log main;
	}
	 
	#https代理https服务(适用tomcat配有https)
	location /license-admin-xxx/ {
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header Host $http_host;
		proxy_set_header X-Forwarded-Proto https;
		proxy_redirect off;
		proxy_pass https://admin-hosts;
		proxy_connect_timeout 800s;
		proxy_send_timeout 800s;
		proxy_read_timeout 800s;
		error_log /usr/local/nginx/logs/admin_https_error.log warn;
		access_log /usr/local/nginx/logs/admin_https_ccess.log main;
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

ok nginx 配置 https 到这就结束了

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号