前端工程配置Nginx反向代理[通俗易懂]

前端工程配置Nginx反向代理HTTP配置HTTPS配置配置两个反向代理,一个代理http页面,一个代理https页面,前者监听80端口,后者监听443端口。配置后整个文件如下,其中有不少冗余,挑有用的看即可。#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid

大家好,又见面了,我是全栈君。

前端开发时,想要通过正式域名直接访问到本地的开发环境,可以通过配置反向代理的形式来实现,如果开了反向代理,就走本地,不开则走线上。

配置两个反向代理,一个代理http页面,一个代理https页面,前者监听80端口,后者监听443端口。配置后整个文件如下,其中有不少冗余,挑有用的看即可。


#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   html;
            index  index.html index.htm;
            proxy_pass http://localhost:3000/;
        }

        #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 { 
   
        #监听443端口。443为知名端口号,主要用于HTTPS协议
        listen       443 ssl;
 
        #定义使用www.xx.com访问
        server_name  dev.wapa.taobao.com;
 
        #ssl证书文件位置(常见证书文件格式为:crt/pem)
        ssl_certificate      /Users/liuzhenhui/Documents/Key/ssl.crt;
        #ssl证书key位置
        ssl_certificate_key  /Users/liuzhenhui/Documents/Key/ssl.key;
 
        #ssl配置参数(选择性配置)
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        #数字签名,此处使用MD5
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
 
        
        #编码格式
        charset utf-8;
        
        #代理配置参数
        proxy_connect_timeout 180;
        proxy_send_timeout 180;
        proxy_read_timeout 180;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarder-For $remote_addr;
 
        #反向代理的路径(和upstream绑定),location 后面设置映射的路径
        location / { 
   
            proxy_ssl_server_name on;
            proxy_pass https://localhost:3000/;
        } 
 
        #静态文件,nginx自己处理
        location ~ ^/(images|javascript|js|css|flash|media|static)/ { 
   
            root C:/XMCARES_X/WorkSpace/nginx/src/main/webapp/views;
            #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
            expires 30d;
        }
    
        #设定查看Nginx状态的地址
        location /NginxStatus { 
   
            stub_status           on;
            access_log            on;
            auth_basic            "NginxStatus";
            auth_basic_user_file  conf/htpasswd;
        }
    
        #禁止访问 .htxxx 文件
        location ~ //.ht { 
   
            deny all;
        }
        
        #错误处理页面(可选择性配置)
        #error_page 404 /404.html;
        #error_page 500 502 503 504 /50x.html;
        #location = /50x.html { 
   
        # root html;
        #
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server { 
   
    # listen 8000;
    # listen somename:8080;
    # server_name somename alias another.alias;

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


    # HTTPS server
    #
    #server { 
   
    # listen 443 ssl;
    # server_name localhost;

    # ssl_certificate cert.pem;
    # ssl_certificate_key cert.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;
    # }
    #}
    include servers/*;
}

HTTP配置

如上面配置,具体http的配置主要如下

server { 
   
        listen       80;//监听80端口
        server_name  dev.taobao.com;//监听的域名
        //要代理转向的目标配置
        location / { 
   
            root   html;
            index  index.html index.htm;
            proxy_pass http://localhost:3000/;
        }
    }

以上配置表示,nginx监听dev.taobao.com域名的80端口,当有请求来到dev.taobao.com:80时,把这个请求代理到localhost:3000,所以在前端工程启动后,node.js配置的是3000端口,但是浏览器直接访问80端口即可(即默认端口),无需显示指定域名后面的端口,直接访问dev.taobao.com就可以了,这样的好处是,直接用域名就能访问到本地的开发环境。

HTTPS配置


    server { 
   
        #监听443端口。443为知名端口号,主要用于HTTPS协议
        listen       443 ssl;
 
        #定义使用www.xx.com访问
        server_name  dev.taobao.com;
 
        #ssl证书文件位置(常见证书文件格式为:crt/pem)
        ssl_certificate      /Users/liuzhenhui/Documents/Key/ssl.crt;
        #ssl证书key位置
        ssl_certificate_key  /Users/liuzhenhui/Documents/Key/ssl.key;

配置项跟http差不多,多了两条ssl的配置,首先生成证书,然后把证书信息填上即可,证书生成流程如下:

  1. 生成rsa私钥,des3算法,1024位强度,ssl.key是秘钥文件名。
    openssl genrsa -des3 -out ssl.key 1024
  2. 输入密码两次,填写一样即可,可随意填写,下一步就会删除
  3. 删除密码
    openssl rsa -in ssl.key -out ssl.key
  4. 生成CSR
    openssl req -new -key ssl.key -out ssl.csr
    需要输入一些信息,随便填写即可
  5. 生成自签名证书
    //这里3650是证书有效期(单位:天)
    openssl x509 -req -days 3650 -in ssl.csr -signkey ssl.key -out ssl.crt

证书生成完成,配置到文件中,既可以直接用https://dev.taobao.com来访问本地的开发环境了。

如果是mac系统,因为证书是自己的,需要添加到系统的信赖证书才能用,否则浏览器会拦截请求。用safari打开https://dev.taobao.com会提示证书不可用,这是选择证书信任,然后强制加载,就会把证书加入到信任列表了。

参考
https://www.cnblogs.com/shuiche/p/13557475.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/121300.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java简单入门教程_史上最快速最简单的java入门教程

    java简单入门教程_史上最快速最简单的java入门教程原标题:史上最快速最简单的java入门教程§java是什么?Java是一门开发软件的程序语言,用于编写比如:办公自动化软件、超市的销售系统、银行交易系统等程序软件.§准备写Java程序。准备工作分为三步:第一步:下载安装JDK开发环境(建议官网下载),基本都是下一步。第二步:配置JDK环境变量,主要配置bin和classpath(主要为了在控制台能方便执行程序。如果不怕麻烦,此步可以省略)。具体…

    2022年7月8日
    22
  • 三期_day12_其它+jetty的使用

    三期_day12_其它+jetty的使用

    2022年2月3日
    41
  • linux udp编程 绑定失败_udp socket编程

    linux udp编程 绑定失败_udp socket编程简介UDP协议与TCP协议一样用于处理数据包,在OSI模型中,两者都位于传输层,处于IP协议的上一层。UDP有不提供数据包分组、组装和不能对数据包进行排序的缺点,也就是说,当报文发送之后,是无法得知其是否安全完整到达的。代码实现#ifndef_SOCKET_HPP_#define_SOCKET_HPP_#include<iostream>#include<sstream>#include<exception>#include<strin

    2022年9月7日
    2
  • VS2005 SP1补丁下载与安装(转摘)「建议收藏」

    VS2005 SP1补丁下载与安装(转摘)「建议收藏」1.先从微软网站下载补丁.    下载地址1为:http://download.microsoft.com/download/6/3/c/63c69e5d-74c9-48ea-b905-30ac3831f288/VS80sp1-KB926601-X86-ENU.exe(英文版)    下载地址2为:http://download.microsoft.com/download

    2022年10月5日
    1
  • spark scheduler_scheduledthreadpool

    spark scheduler_scheduledthreadpoolSpark的TaskScheduler和DagScheduler开始研究神奇的spark。会陆续将研究的心得放上来。在Spark中一个核心的是模块就是调度器(Scheduler),在spark中Scheduler有两种TaskScheduler(是低级的调度器接口),DagScheduler(是高级的调度)我们在创建SparkContext对象的时候,sparkcontext内部就会创建Ta…

    2022年10月10日
    4
  • ADRC自抗扰控制自学笔记(包含simulink仿真)(转载)

    ADRC自抗扰控制自学笔记(包含simulink仿真)(转载)摘自:https://blog.csdn.net/zouxu634866/article/details/106287879#comments_12978720ADRC自抗扰控制自学笔记(包含simulink仿真)总被蚊子叮的小旭2020-05-2217:59:361856收藏28分类专栏:控制版权ADRC控制中包含三个主要的部分:跟踪微分器,非线性状态反馈(非线性组合),扩张观测器。ADRC特点:继承了经典PID控制器的精华,对被控对…

    2022年5月19日
    79

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号