下载证书,可自行在阿里云申请
有两个文件:
1、证书名称.key
2、证书名称.pem
在nginx安装目录新建cert目录,并放入证书文件
#监听http自动转https
server {
listen 80; #监听80端口
server_name www.abc.com; #监听的域名
rewrite ^(.*)$ https://$host$1 permanent;#http自动跳转到https
}
server {
#侦听443端口,这个是ssl访问端口
listen 443 ssl; #SSL协议访问端口号为443。此处如未添加ssl,可能会造成Nginx无法启动。
server_name www.abc.com; #监听的域名
index index.html index.htm index.php;
root /var/www/web/;
access_log /var/log/nginx/access.log main;
ssl_certificate /etc/nginx/cert/证书名称.pem; #将domain name.pem替换成您证书的文件名。
ssl_certificate_key /etc/nginx/cert/证书名称.key; #将domain name.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 / {
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite /admin.php(.*)$ /admin.php?s=$1 last;#如果有后台入口的情况这样定义
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000; #fastcgi服务端口,将http请求代理到此端口
fastcgi_index index.php; #fastcgi服务默认页面
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #设置请求的脚本文件路径
include fastcgi_params; # 引入fastcgi的配置文件
}
#静态文件,nginx自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
#过期30天,静态文件不怎么更新,过期可以设大一点,
#如果频繁更新,则可以设置得小一点。
expires 30d;
}
#禁止访问 .htxxx 文件
# location ~ /.ht {
# deny all;
#}
}
配置好后运行nginx -t查看配置是否OK
nginx -t 重启nginx
#停止服务
systemctl stop nginx
#开启服务
systemctl start nginx 配置结束。
补充全局nginx.conf配置含义
# 运行用户,默认即是nginx,可以不进行设置
user nginx;
#Nginx进程,一般设置为和CPU核数一样
worker_processes 1;
#错误日志存放目录
error_log /var/log/nginx/error.log warn;
#进程pid存放位置
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; #nginx访问日志存放位置
sendfile on; #开启高效传输模式
#tcp_nopush on; #减少网络报文段的数量
keepalive_timeout 65; #保持连接的时间,也叫超时时间
#gzip on; #开启gzip压缩
include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件
}