Windows下用Nginx配置https服务器及反向代理的问题


Posted in Servers onSeptember 25, 2021

请求逻辑

前端 --> https方式请求nginx
nginx --> 通过http请求后端服务

安装OpenSSL

下载地址

Windows下用Nginx配置https服务器及反向代理的问题

然后配置环境变量。在系统环境变量中添加环境变量:

变量名:OPENSSL_HOME

变量值:F:\OpenSSL-Win64\bin;

(变量值为OPENSSL安装位置下的bin目录)

生成证书

用命令行随便打开一个目录, 使用如下命令生成证书

# 创建私钥
# test文件名是自己随便起即可, 这个命令会让你设置两次rsa的密码, 请务必记住该密码, 后续需要使用, 命令执行完毕, 会在当前目录生成 test.key 的文件
openssl genrsa -des3 -out test.key 1024     

# 创建csr证书, 这里用到的 test.key 是上一个命令生成的那个. 执行这个命令后,需要输入一系列的信息。输入的信息中最重要的为Common Name,这里输入的域名即为我们要使用https访问的域名 ,比如我输入的是localhost。其它的内容随便填即可。以上步骤完成后,ssl文件夹内出现两个文件:test.csr 和 test.key
openssl req -new -key test.key -out test.csr

# 去除密码
# 在加载SSL支持的Nginx并使用上述私钥时除去必须的口令,否则会在启动nginx的时候需要输入密码。
# 复制test.key并重命名为test.copy.key
# 在命令行中执行如下命令以去除口令(此时需要输入密码,这个密码就是上文中在创建私钥的时候输入的密码。)
openssl rsa -in test.copy.key -out test.key

# 生成crt证书. 证书生成完毕。我们发现,ssl文件夹中一共生成了4个文件。下面,配置https服务器的时候,我们需要用到的是其中的test.crt和test.key这两个文件。
openssl x509 -req -days 365 -in test.csr -signkey test.key -out test.crt

Windows下用Nginx配置https服务器及反向代理的问题
Windows下用Nginx配置https服务器及反向代理的问题
Windows下用Nginx配置https服务器及反向代理的问题

下载安装nginx, 修改nginx配置

将生成的test.keytest.crt 移动到 $NGINX_ROOT/conf目录

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   D:/local-site;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    server {
       listen       8086;
       listen       localhost:8086;
       server_name  localhost;
       gzip on;
       gzip_buffers 4 16k;
       gzip_comp_level 6;
       gzip_vary on;
       gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/xml+rss text/javascript;

       location / {
           root   D:/local-site/good-test;
           index  index.html index.htm;
       }

       location ^~/api/ { 
           rewrite ^~/api/(.*)$ /$1 break;
           proxy_pass   http://localhost:8080/; #代理IP:端口
       }
    }

    # HTTPS server 配置, 这里使用了反向代理和跨域支持, 注意nginx和后端服务, 只需要在nginx设置跨域即可, 后端服务的跨域不要开启, 如果两边都开启了跨域, 会出问题
    #
    server {
       listen       443 ssl;
       server_name  localhost;

       ssl_certificate      test.crt;
       ssl_certificate_key  test.key;

       ssl_session_cache    shared:SSL:1m;
       ssl_session_timeout  5m;

       ssl_ciphers  HIGH:!aNULL:!MD5;
       ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }

       location / { 
        #    rewrite ^~/api/(.*)$ /$1 break;
            # add_header Access-Control-Allow-Origin *;
            # 允许客户端的请求方法
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
            # 允许客户端提交的的请求头
            add_header 'Access-Control-Allow-Headers' 'Origin, x-requested-with, Content-Type, Accept, Authorization';
            # 允许客户端提交Cookie
            add_header 'Access-Control-Allow-Credentials' 'true';
            # 允许客户端访问的响应头
            add_header 'Access-Control-Expose-Headers' 'Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma';

            proxy_pass   http://10.114.119.61:8080;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Port $server_port;

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
       }
    }

    server {
       listen       8443 ssl;
       server_name  localhost;

       ssl_certificate      test.crt;
       ssl_certificate_key  test.key;

       ssl_session_cache    shared:SSL:1m;
       ssl_session_timeout  5m;

       ssl_ciphers  HIGH:!aNULL:!MD5;
       ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }

       location / { 
        #    rewrite ^~/api/(.*)$ /$1 break;
            # add_header Access-Control-Allow-Origin $http_origin;
            # 允许客户端的请求方法
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
            # 允许客户端提交的的请求头
            add_header 'Access-Control-Allow-Headers' 'Origin, x-requested-with, Content-Type, Accept, Authorization';
            # 允许客户端提交Cookie
            add_header 'Access-Control-Allow-Credentials' 'true';
            # 允许客户端访问的响应头
            add_header 'Access-Control-Expose-Headers' 'Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma';

            # 这是是配置需要代理的服务
            proxy_pass   http://10.114.119.61:7001;
            # proxy_pass https://172.16.46.38:8443;
            # proxy_pass   http://10.114.119.61:8866;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Port $server_port;

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
       }
    }

}

重启nginx

本地域名配置

打开C:\Windows\System32\drivers\etc\hosts文件

加入配置:

10.114.119.61 pan.test.com
10.114.119.61 pan.uat.com

到此这篇关于Windows下用Nginx配置https服务器及反向代理的文章就介绍到这了,更多相关Nginx配置https服务器内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Servers 相关文章推荐
详解如何修改nginx的默认端口
Mar 31 Servers
Nginx进程管理和重载原理详解
Apr 22 Servers
nginx结合openssl实现https的方法
Jul 25 Servers
图文详解Nginx版本平滑升级方案
Sep 15 Servers
Windows下用Nginx配置https服务器及反向代理的问题
Sep 25 Servers
Nginx配置根据url参数重定向
Apr 11 Servers
openstack云计算keystone组件工作介绍
Apr 20 Servers
如何Tomcat中使用ipv6地址
May 06 Servers
详解如何使用Nginx解决跨域问题
May 06 Servers
利用Apache Common将java对象池化的问题
Jun 16 Servers
Nginx报404错误的详细解决方法
Jul 23 Servers
win10搭建配置ftp服务器的方法
Aug 05 Servers
nginx安装以及配置的详细过程记录
Sep 15 #Servers
Nginx缓存设置案例详解
Sep 15 #Servers
图文详解Nginx版本平滑升级方案
Sep 15 #Servers
Nginx配置文件详解以及优化建议指南
Sep 15 #Servers
学习nginx基础知识
Nginx location 和 proxy_pass路径配置问题小结
Sep 04 #Servers
Nginx使用Lua模块实现WAF的原理解析
You might like
php类中private属性继承问题分析
2012/11/01 PHP
PHP中::、->、self、$this几种操作符的区别介绍
2013/04/24 PHP
php网站判断用户是否是手机访问的方法
2013/11/01 PHP
php实现无限级分类
2014/12/24 PHP
php表单处理操作
2017/11/16 PHP
js 单引号 传递方法
2009/06/22 Javascript
jquery下json数组的操作实现代码
2010/08/09 Javascript
探索Emberjs制作一个简单的Todo应用
2012/11/07 Javascript
浮动的div自适应居中显示的js代码
2013/12/23 Javascript
利用jQuary实现文字浮动提示效果示例代码
2013/12/26 Javascript
JS中attr和prop属性的区别以及优先选择示例介绍
2014/06/30 Javascript
js防止DIV布局滚动时闪动的解决方法
2014/10/30 Javascript
JavaScript 总结几个提高性能知识点(推荐)
2017/02/20 Javascript
Javascript中将变量转换为字符串的三种方法
2017/09/19 Javascript
vue+iview写个弹框的示例代码
2017/12/05 Javascript
Node.js npm命令运行node.js脚本的方法
2018/10/10 Javascript
IE11下处理Promise及Vue的单项数据流问题
2019/07/24 Javascript
vuejs移动端实现div拖拽移动
2019/07/25 Javascript
js实现点击烟花特效
2020/10/14 Javascript
vue项目配置 webpack-obfuscator 进行代码加密混淆的实现
2021/02/26 Vue.js
Python with的用法
2014/08/22 Python
python实现2048小游戏
2015/03/30 Python
Python中自定义函数的教程
2015/04/27 Python
python使用 zip 同时迭代多个序列示例
2019/07/06 Python
python实现切割url得到域名、协议、主机名等各个字段的例子
2019/07/25 Python
使用python检查yaml配置文件是否符合要求
2020/04/09 Python
Kears 使用:通过回调函数保存最佳准确率下的模型操作
2020/06/17 Python
Python特殊属性property原理及使用方法解析
2020/10/09 Python
Python Django路径配置实现过程解析
2020/11/05 Python
芬兰设计商店美国:Finnish Design Shop US
2019/03/25 全球购物
印度第一网上礼品店:IGP.com
2020/02/06 全球购物
Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别?
2014/07/27 面试题
医学护理毕业生自荐信
2013/11/07 职场文书
社区包粽子活动方案
2014/01/21 职场文书
五五普法心得体会
2014/09/04 职场文书
钓鱼岛事件感想
2015/08/11 职场文书