使用Nginx实现https请求转发http

时间:2025-03-19 21:32:20

使用Nginx实现https请求转发http

在项目正式上线时,一般会申请域名和证书来实现https的服务,这种情况我们使用Nginx的代理功能即可完美实现上线需求,无需修改项目;话不多说,上干货。
使用nginx转发https请求需要nginx安装插件:
在安装nginx的时候,编译时需要安装ssl插件
./configure --prefix=/nginx安装路径 --with-http_stub_status_module --with-http_ssl_module --with-stream_ssl_module
然后正常执行完nginx安装步骤即可
Nginx的安装与配置这里省略,我这里主要说明https请求代理成http如何配置,进入nginx安装目录,找到配置文件,进入编辑(vi )

server {
        listen       8094 ssl;     #监听端口号--即位代理后的端口号
        server_name  localhost;
        client_max_body_size 100m;
        ssl_certificate      /usr/local/nginx-1.19.4/conf/;       #证书公钥路径
        ssl_certificate_key  /usr/local/nginx-1.19.4/conf/;  #证书私钥路径
        location /{
            client_max_body_size 100m;
            proxy_pass http://127.0.0.1:9004;     #项目地址和端口号
        }
         error_page   500 502 503 504  /;
        location = / {
            root   html;
        }
}

上述这一段配置即为,当访问8094端口时,例如,我nginx的服务器IP为198.32.23.23,域名是,当我们访问.8094时,实际是通过nginx转发到了http://127.0.0.1:9004这个地址,即为我们项目的地址;就实现了https到http的转发