nginx如何添加https访问 ?
原始http
server {
listen 80;
server_name 0.0.0.0 www.xxxxxx.cn;
# 拒绝非 HTTP 请求(如二进制数据)
if ($request_method !~ ^(GET|POST|HEAD)$ ) {
return 444;
}
# 拒绝异常的 User-Agent(空或非法字符)
if ($http_user_agent ~ ^$|"\\x|\\x|[\x00-\x1F]) {
return 403;
}
location / {
alias /ipu-data/data_openresty/html/;
autoindex off;
index authentication.html;
}
location ~ ^/html/ {
root /ipu-data/data_openresty;
try_files /html/authentication.html =404;
autoindex off;
}
location /aaa/ {
proxy_pass http://127.0.0.1:17077/aaa/;
}
}
修改成https后
server {
listen 80;
server_name 0.0.0.0 www.xxxxxx.cn;
return 301 https://$host$request_uri;
{
server {
listen 443 ssl;
server_name www.xxxxxx.cn;
proxy_send_timeout 1800;
proxy_read_timeout 1800;
proxy_connect_timeout 1800;
root html;
index index.html index.htm;
ssl_certificate /usr/local/openresty/nginx/conf/cert/www.xxxxxx.cn;
ssl_certificate_key /usr/local/openresty/nginx/conf/cert/www.xxxxxx.cn;
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;
# 拒绝非 HTTP 请求(如二进制数据、就是防止别人恶意攻击、恶意请求)
if ($request_method !~ ^(GET|POST|HEAD)$ ) {
return 444;
}
# 拒绝异常的 User-Agent(空或非法字符)
if ($http_user_agent ~ ^$|"\\x|\\x|[\x00-\x1F]) {
return 403;
}
location / {
alias /ipu-data/data_openresty/html/;
autoindex off;
index authentication.html;
}
#访问这个接口、
location ~ ^/test/ {
root /ipu-data/data_openresty;
try_files /html/authentication.html =404;
autoindex off;
}
#访问这个接口走默认路径
location ~ ^/html/ {
root /ipu-data/data_openresty;
try_files /html/authentication.html =404;
autoindex off;
}
#访问这个接口由java后端服务来处理。
location /aaa/ {
proxy_pass http://127.0.0.1:17077/aaa/;
}
}
案例:如果域名是www.jigaobo.xyz
- 证书和私钥路径
- 您需要将 SSL 证书和私钥文件放在服务器上,并更新 Nginx 配置中的路径。假设您的证书和私钥文件位于
/usr/local/openresty/nginx/conf/cert/ 目录下,并且文件名与您的域名相匹配(例如 www.jigaobo.xyz.crt 和 www.jigaobo.xyz.key)。
- 服务器名称
- 监听端口和重定向
- 确保您的服务器监听 443 端口用于 HTTPS 连接,并且所有 HTTP 请求都被重定向到 HTTPS。