本文以docker的镜像的形式启动nginx服务并配置阿里云ssl证书,以下为操作过程
- 确保你的云服务器安装了docker,并下载好nginx镜像。不同版本后面配置稍有不同,各位看官随意
- 创建配置文件,目录可根据自身喜好自行设置
mkdir -p /root/nginx/conf
cd /root/nginx/conf
vim nginx.conf
- 下载阿里云ssl证书,免费的自己申请,然后下载nginx版本的key和pem到/etc/nginx/cert/下,然后编辑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;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.xxx;#这里的域名换成你自己的
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
#不同版本写法不同,我这是最新版就写这个如果不行就试试 以下
#listen 443;
#ssl on;
server_name www.xxx;#这里的域名换成你自己的
index index.html;
ssl_certificate /etc/nginx/cert/.pem;#写你的证书pem
ssl_certificate_key /etc/nginx/cert/.key;#写你的证书key
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
expires off;
sendfile off;
proxy_pass http://www;#这里的域名换成你自己的
}
}
upstream www.ylbx.work{
server 127.0.0.1:8080;#意思就是说80端口给你代理到8080
}
}
- 起nginx服务
docker run
-p 80:80
--name c_nginx
-p 443:443
-v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
-v /root/nginx/logs:/var/log/nginx
-v /root/nginx/html:/usr/share/nginx/html
-v /root/nginx/cert:/etc/nginx/cert
nginx
-
把你的项目起在8080端口,然后直接访问你的域名即可实现转https到你的项目,记得别加端口。 -
Done!搞定收工
诺克萨斯最高统帅部:看完不点赞收藏,明天我的斧头就会劈到你家门口!!
|