nginx+acme申请证书
- centos上执行以下命令
yum update #更新服务器一些依赖
yum install nginx #安装Nginx
nginx -c /etc/nginx/nginx.conf #执行这句之后reload不会报错
vim /etc/nginx/nginx.conf #打开nginx配置文件
#把里面的 server_name 改为你想要的域名
#下面继续安装acme
curl https://get.acme.sh | sh #如果卡的话 ctrl+c 取消再执行,多试几次
cd ~/.acme.sh
./acme.sh --issue -d mydomain.com --nginx #替换mydomain.com为你的域名
#成功后会显示证书的路径,这里我们只需要 mydomain.key 和 fullchain.cer 把这两个的路径复制到nginx.conf上就配置成功了
下面是nginx.conf的配置示例 以强制跳转https
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name mydomain.com;
return 301 https://$server_name$request_uri;
}
# Settings for a TLS enabled server.
#
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name _;
#如果是配置静态网站 注解掉下面这行 改为你的网站
#root /usr/share/nginx/html;
#这里是配置fullchain.cer的路径
ssl_certificate "/root/.acme.sh/mydomain.com/fullchain.cer";
#这里是配置mydomain.key的路径
ssl_certificate_key "/root/.acme.sh/mydomain.com/mydomain.com.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# include /etc/nginx/default.d/*.conf;
location / {
#配置静态网站注释掉下面这行,接口的话就像下面这样写
proxy_pass http://127.0.0.1:8090;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
License:
CC BY 4.0