Ubuntu Nginx https 配置

本文详细介绍如何使用Nginx配置HTTP至HTTPS的跳转,包括80端口至443端口的重定向设置,SSL证书的正确放置路径,以及防火墙配置等关键步骤。同时提供错误排查方法,适用于多域名HTTPS配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#配置http跳转到https 80跳转443
server { listen
80; server_name www.***.com www.***.cn; return 301 https://$server_name$request_uri; }
#配置https server { listen
443; server_name www.***.com www.***.cn; index index.html index.php; root /var/www/php/demo; ssl on; ssl_certificate https_keys/1_www.***.com_bundle.crt; ssl_certificate_key https_keys/2_www.***.com.key; error_log /log/error/error.log; access_log /log/access/access.log; include common.conf; }

 上面的代码是 nginx server 中的配置 https 配置 如果访问的是http 直接跳转 https

我将https 协议的 key 和crt 文件到了https_keys 中 

几点注意事项:
1. 如果HTTPS方式访问网站出现无响应的情况,可以看看自己的服务器防火墙配置,是不是需要将443端口加到例外中去; 
2. 注意自己xxx.pemxxx.key的存放路径,区分相对路径和决定路径使用方式; 
3. 通过Nginx安装目录下的logs文件夹中的access.logerror.log日志文件来进行排错; 
4. 同一个Nginx可以配置多个HTTPS的域名,只需要将上面443端口的监听配置复制一份加到配置文件中,然后修改好相应的server_namexxx.pemxxx.key文件路径即可。

 

转载于:https://www.cnblogs.com/wjm956/p/9531555.html

### 配置 NginxUbuntu 上支持 HTTPS 要在 Ubuntu 系统中配置 Nginx 以支持 HTTPS,需完成以下几个方面的操作: #### 1. 安装必要的软件包 确保已安装 OpenSSL 和其他依赖项。如果尚未安装 Nginx,则可以通过以下命令安装: ```bash sudo apt update && sudo apt install nginx openssl -y ``` #### 2. 启用 SSL 模块 在编译 Nginx 时需要启用 `--with-http_ssl_module` 参数[^2]。大多数情况下,默认的 Nginx 软件包已经包含了此模块。可通过以下命令验证是否启用了该模块: ```bash nginx -V 2>&1 | grep -- '--with-http_ssl_module' ``` 如果没有找到相关内容,则可能需要重新编译 Nginx 或者通过官方源安装。 #### 3. 获取 SSL/TLS 证书 获取有效的 SSL/TLS 证书对于 HTTPS 至关重要。可以选择 Let's Encrypt 提供的免费证书或其他商业 CA 的证书。以下是使用 Certbot 自动化工具申请 Let's Encrypt 证书的方法: ```bash sudo add-apt-repository ppa:certbot/certbot sudo apt-get update sudo apt-get install python-certbot-nginx sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com ``` 这会自动创建并部署所需的证书文件到 `/etc/letsencrypt/live/yourdomain.com/` 下[^4]。 #### 4. 创建 Diffie-Hellman 参数文件 为了增强加密强度,推荐生成一个自定义的 Diffie-Hellman 参数文件: ```bash sudo openssl dhparam -out /etc/nginx/dhparam.pem 2048 ``` 注意:上述过程可能会花费几分钟时间来计算[^1]。 #### 5. 修改 Nginx 配置文件 编辑站点对应的虚拟主机配置文件(通常位于 `/etc/nginx/sites-available/default`),添加如下内容以便加载新证书以及优化安全设置: ```nginx server { listen 80; server_name yourdomain.com www.yourdomain.com; # Redirect all HTTP requests to HTTPS with a permanent redirect (status code 301). return 301 https://$host$request_uri; } server { listen 443 ssl http2; server_name yourdomain.com www.yourdomain.com; ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; ssl_dhparam /etc/nginx/dhparam.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256'; ssl_prefer_server_ciphers on; location / { proxy_pass http://localhost:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } error_page 497 https://$host:$server_port$request_uri; } ``` 以上代码片段设置了强制重定向至 HTTPS 并指定了具体的 SSL 设置参数。 #### 6. 测试配置并重启服务 保存更改后的配置文件后,测试语法正确性再启动服务: ```bash sudo nginx -t sudo systemctl restart nginx ``` 最后确认防火墙允许端口 443 访问即可正常工作。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值