【Nginx】使用自生成证书配置nginx代理https

使用Nginx代理HTTPS请求并使用自签名证书,可以按照以下步骤进行配置:

  1. 生成自签名证书:

    • 打开终端或命令提示符,并导航到Nginx配置文件所在的目录。
    • 运行以下命令生成自签名证书和私钥:

    |---|------------------------------------------------------------------------------------|
    | | openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ssl.key -out ssl.crt |

    • 根据提示输入证书的相关信息,例如国家、省份、城市、组织等。
  2. 配置Nginx代理:

    • 打开Nginx配置文件(通常为nginx.conf)。
    • http块中添加一个新的server块,用于代理HTTPS请求。示例配置如下:

    |---|-----------------------------------------------------------------|
    | | nginx`server { |
    | | listen 80; |
    | | server_name example.com; |
    | | |
    | | location / { |
    | | proxy_pass http://backend_server_address; |
    | | proxy_set_header Host $host; |
    | | proxy_set_header X-Real-IP $remote_addr; |
    | | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
    | | } |
    | | } |
    | | |
    | | server { |
    | | listen 443 ssl; |
    | | server_name example.com; |
    | | |
    | | ssl_certificate ssl.crt; |
    | | ssl_certificate_key ssl.key; |
    | | ssl_protocols TLSv1 TLSv1.1 TLSv1.2; |
    | | ssl_ciphers 'HIGH:!aNULL:!MD5:!kEDH'; |
    | | ssl_session_cache shared:SSL:10m; |
    | | ssl_session_timeout 10m; |
    | | |
    | | location / { |
    | | proxy_pass http://backend_server_address; |
    | | proxy_set_header Host $host; |
    | | proxy_set_header X-Real-IP $remote_addr; |
    | | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
    | | } |
    | | ``}``` |

    • example.com替换为你的域名,backend_server_address替换为后端服务器的地址。确保将生成的证书文件(ssl.crtssl.key)的路径替换为正确的路径。
  3. 保存并关闭配置文件。

  4. 检查Nginx配置是否正确:运行以下命令,查看Nginx的错误日志中是否有任何错误:

|---|--------------------------------------------------------------------------------------------------|
| | nginx -t``` |
| | 5. 重启Nginx服务,使更改生效: |
| | ```````arduino | | |service nginx restart``` ```` |
| | 或者根据你所使用的系统,使用相应的命令重启Nginx服务。例如,在Ubuntu上可以使用`sudo service nginx restart`。 |
| | `6. 确保域名解析和证书的一致性:确保你的域名解析正确,并且指向代理服务器的IP地址。同时,确保在浏览器中访问时使用的是正确的域名,而不是IP地址。证书只有在正确使用域名访问时才会被信任。` |

相关推荐
智塑未来4 分钟前
2025-2026年具身智能机器人自动化程度综合评测:五大品牌自研大模型与操作系统全对比
运维·机器人·自动化
AI_零食5 分钟前
奶茶大数据运维表 - 鸿蒙PC Electron框架技术实现详解
运维·前端·华为·electron·开源·harmonyos·鸿蒙
Shadow(⊙o⊙)6 分钟前
System V共享内存详解,shm系列接口,三种共享内存删除机制。System V通信缺点分析
linux·运维·服务器·开发语言·网络·c++
morning_judger7 分钟前
Agent开发系列(七)-可观测性Agent的设计
运维·人工智能
运维小欣12 分钟前
智能运维监控厂商深度选型推荐
运维
万能的知了12 分钟前
服务器托管 vs 云主机 vs 裸金属:一张决策流程图
运维·服务器·网络
喵喵爱自由13 分钟前
ubuntu离线扩展磁盘分区
linux·运维·ubuntu
跨境小彭14 分钟前
2026跨境电商精细化洗牌:破解利润核算与多店运维痛点,实操工具全解析
大数据·运维·信息可视化·跨境电商·temu·temu电商运营
RisunJan19 分钟前
Linux命令-openssl(强大的安全套接字层密码库)
linux·运维·服务器
RisunJan9 小时前
Linux命令-nologin(用于系统账户或需要禁止交互式登录的场景)
linux·运维