nginx使用自签名SSL证书
# nginx 使用自签名 SSL 证书
# 创建 SSL 证书
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/your-selfsigned.key -out /etc/ssl/certs/your-selfsigned.crt
1
# nginx 配置 SSL
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bk
sudo vim /etc/nginx/sites-available/default
# 将请求重定向到https
# 启动https服务
1
2
3
4
2
3
4
完整的配置如下:
# 将your_ip和ssl密钥文件替换
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name your_ip;
return 301 https://$server_name$request_uri;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
root /var/www;
index index.html index.htm index.nginx-debian.html;
ssl_certificate /etc/ssl/certs/your-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/your-selfsigned.key;
location / {
try_files $uri $uri/ =404;
}
}
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
71
72
73
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
71
72
73
重启 nginx 服务
service nginx restart
1
上次更新: 2022/06/17, 07:22:19