Linux下Nginx安装证书
Posted raok
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下Nginx安装证书相关的知识,希望对你有一定的参考价值。
个人博客地址: https://note.raokun.top
拥抱ChatGPT,国内访问网站:https://www.playchat.top
1.服务器自带nginx修改配置
1.查看Nginx进程:
ps -aux | grep nginx
2.修改对应config文件
vim /www/server/nginx/conf/nginx.conf
修改内容:
server
#SSL 默认访问端口号为 443
listen 443 ssl;
#请填写绑定证书的域名
server_name cloud.tencent.com;
#请填写证书文件的相对路径或绝对路径
ssl_certificate cloud.tencent.com_bundle.crt;
#请填写私钥文件的相对路径或绝对路径
ssl_certificate_key cloud.tencent.com.key;
ssl_session_timeout 5m;
#请按照以下协议配置
ssl_protocols TLSv1.2 TLSv1.3;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location /
#网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
#例如,您的网站主页在 Nginx 服务器的 /etc/www 目录下,则请修改 root 后面的 html 为 /etc/www。
root html;
index index.html index.htm;
location /portainer/
proxy_pass http://1.15.118.16:9000/; #代理链接的portainer web端口
3.在 Nginx 根目录下,通过执行以下命令验证配置文件问题。
./sbin/nginx -t
4.在 Nginx 根目录下,通过执行以下命令重载 Nginx。
./sbin/nginx -s reload
5.重载成功,即可使用 https://cloud.tencent.com
进行访问。
2.docker创建nginx配置SSL
1.docker创建Nginx
mkdir -p /data/nginx/conf,conf.d,html,logs,certs
a、将上面下载的证书解压之后,上传到/data/nginx/certs目录下
b、在/data/conf文件下创建nginx.conf文件
user nginx;
worker_processes auto; #一般为cpu核数
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events
worker_connections 1024;
http
include /etc/nginx/mime.types;
default_type application/octet-stream;
#log格式
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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on; #开启压缩
include /etc/nginx/conf.d/*.conf;
c、在/data/html文件下创建html文件 index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html color-scheme: light dark;
body width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
d、在/data/nginx/conf.d/目录创建default.conf
server
listen 80;
listen [::]:80;
server_name www.example.com; #填写域名
#将所有HTTP请求通过rewrite指令重定向到HTTPS
rewrite ^(.*) https://$server_name$1 permanent;
#配置443端口
server
listen 443 ssl; # 1.1版本后这样写
server_name www.example.com; #填写域名
ssl_certificate certs/1_www.example.com.pem; #需要将cert-file-name.pem替换成已上传的证书文件的名称。
ssl_certificate_key certs/1_www.example.com.key; #需要将cert-file-name.key替换成已上传的证书私钥文件的名称。
ssl_session_timeout 5m;
#表示使用的加密套件的类型。
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #表示使用的TLS协议的类型。
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_session_cache shared:SSL:1m;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
location /
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root html;
index index.html index.htm;
e、授权文件给nginx用户
chown -R nginx:nginx /data/nginx
f、创建容器并启动
docker run --name nginx -d -p 80:80 \\
-p 443:443 \\
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \\
-v /data/nginx/conf.d/:/etc/nginx/conf.d \\
-v /data/nginx/html:/etc/nginx/html \\
-v /data/nginx/logs:/var/log/nginx \\
-v /data/nginx/certs:/etc/nginx/certs \\
-v /etc/localtime:/etc/localtime:ro \\
nginx:1.21.4
3.同域名多端口网站映射配置
修改config
server
listen 80;
listen [::]:80;
server_name rao.top; #填写域名
#将所有HTTP请求通过rewrite指令重定向到HTTPS
rewrite ^(.*) https://$server_name$1 permanent;
#配置443端口
server
listen 443 ssl; # 1.1版本后这样写
server_name raokun.top www.rao.top; #填写域名
#请填写证书文件的相对路径或绝对路径
ssl_certificate /etc/nginx/cert/1_raokun.top_bundle.crt;
#请填写私钥文件的相对路径或绝对路径
ssl_certificate_key /etc/nginx/cert/2_raokun.top.key;
ssl_session_timeout 5m;
#表示使用的加密套件的类型。
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #表示使用的TLS协议的类型。
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_session_cache shared:SSL:1m;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
location /
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root html;
index index.html index.htm;
location /raokun
proxy_pass http://1.15.11.16:8090/;
location /portainer/
proxy_pass http://1.15.118.1:9000/; #代理链接的portainer web端口
参考链接:https://blog.csdn.net/weixin_39555954/article/details/124563854
Docker Nginx SSL证书认证启动教程
前言:linux环境下,ssl证书认证https,docker快速部署nginx
docker pull nginx:latest
2 新建文件夹
mkdir -p /home/docker-nginx/conf.d/
mkdir -p /home/docker-nginx/log
mkdir -p /home/docker-nginx/
生成配置nginx.conf文件,文件路径/home/docker-nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream appointment {
# 请将xxx.xxx.xxx.xxx:8080
server xxx.xxx.xxx.xxx:8080 weight=1 max_fails=2 fail_timeout=20;
}
server {
listen 80;
server_name dayuxiaozhi.top;
location / {
root html;
index index.html index.htm;
proxy_pass http://appointment;
}
}
include /etc/nginx/conf.d/*.conf;
}
注意:请将31行的xxx.xxx.xxx.xxx:8080,改成你的ip与端口
生成配置default.conf文件,文件路径为/home/docker-nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log /var/log/nginx/host.access.log main;
location / {
#root /data/nginx/html;
root /usr/share/nginx/html;
index index.html index.htm;
#autoindex on;
#try_files $uri /index/index/page.html;
#try_files $uri /index/map/page.html;
}
#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 /usr/share/nginx/html;
}
location ~ /images {
default_type application/json;
return 200 '{"code": "A000000", "message": "ok", "timestamp": "20180307184426", "data": {"isvip": "1", "monthProList": []}}';
}
# 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;
#}
}
4 启动nginx docker容器
docker run --name nginx -p 80:80 -p 443:443 -v /home/docker-nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/docker-nginx/log:/var/log/nginx -v /home/docker-nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -d nginx
-p 80:80:将容器的 80 端口映射到主机的 80 端口。
-p 443:443:将容器的 80 端口映射到主机的 443 端口。
--name nginx:将容器命名为 nginx。
-v /home/docker-nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf:将服务器上的default.conf 挂载到容器的 /etc/nginx/default.conf
-v /home/docker-nginx/nginx.conf:/etc/nginx/nginx.conf:将服务器上的nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。
-v /home/docker-nginx/log:/var/log/nginx:将服务器上的 logs 挂载到容器的 /var/log/nginx。
5 进入nginx docker容器,新建文件夹/ssl
进入nginx docker前,请检查下容器是否启动,若没有启动,请检查上述步骤是否有缺漏。
docker ps | grep nginx
docker exec -it nginx bash
mkdir /ssl
chmod 775 /ssl
6 证书复制到docker容器中
1_dayuxiaozhi.top_bundle.crt 2_dayuxiaozhi.top.key证书放到/home/docker-nginx,将crt、key证书文件复制到docker容器中
cd /home/docker-nginx
docker cp 2_dayuxiaozhi.top.key nginx:/ssl/
docker cp 1_dayuxiaozhi.top_bundle.crt nginx:/ssl/
7 修改nginx.conf文件
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream appointment {
# 请将xxx.xxx.xxx.xxx:8080
server xxx.xxx.xxx.xxx:8080 weight=1 max_fails=2 fail_timeout=20;
}
server {
listen 80;
server_name dayuxiaozhi.top;
location / {
root html;
index index.html index.htm;
proxy_pass http://appointment;
}
}
server {
listen 443 ssl;
#证书文件名称
ssl_certificate /ssl/1_dayuxiaozhi.top_bundle.crt;
#私钥文件名称
ssl_certificate_key /ssl/2_dayuxiaozhi.top.key;
ssl_session_timeout 5m;
#请按照以下协议配置
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
server_name dayuxiaozhi.top;
location / {
root html;
index index.html index.htm;
proxy_pass http://appointment;
}
}
include /etc/nginx/conf.d/*.conf;
}
注意:请将31行的xxx.xxx.xxx.xxx:8080,改成你的ip与端口
配置两个server,一个是80端口,另外一个是443端口
8 重启docker nginx
docker restart nginx
9 查看重启是否工程
docker ps | grep nginx
以上是关于Linux下Nginx安装证书的主要内容,如果未能解决你的问题,请参考以下文章