Nginx分享

Nginx

 

一、前言

  1. 为毛要用nginx服务器代理,为什么不用tomcat?

  2. 为毛要使用nginx + tomcat + jdk 开发?

  3. 跨域一定要用JSONP或者是CORS(跨域资源共享)?

    常见跨域场景

二、为什么用Nginx?

  1. 热部署

    nginx具有热部署的功能,可以7 * 24小时不间断服务的前提下升级Nginx的可执行文件。同时也可以在不停止服务的情况下修改配置文件、更换日志文件等功能。

  2. 可以高并发链接

    这是一个很重要的特性!随着用户量的不断增加,一些公司会出现高并发请求的问题。只要你的内存ok,nginx处理高并发简直是完美。

  3. 低内存消耗

    在一般的情况下,10000个非活跃的HTTP连接,在nginx仅仅消耗2.5M的内存。

  4. 处理响应请求很快

    正常情况下单次请求会得到很快的响应。在高峰期,nginx可以比其他的web服务器更快的请求。

  5. 前端解决跨域的利器

    对于前端而言解决跨域问题更加方便,仅仅需要修改配置文件,就可以完美解决跨域问题。

三、nginx负载均衡原理图

什么叫负载均衡?

负载均衡就是分摊到多个操作单元上进行执行,从而可以共同的完成工作任务。

四、nginx的安装

nginx下载目录

针对不同的操作系统,安装方式也不同,在此我分享一下Windows系统安装nginx的步骤。

  1. 下载安装包

  2. 解压压缩包

  3. 解压后你会看到这样的内容

  4. 解释一下解压后的主要内容

  • conf ---------- nginx的配置文件

  • html ---------- 前端页面放在这里(需要部署的代码)

  • logs ---------- 顾名思义,nginx的日志文件

  • nginx.exe ---------- nginx执行文件(启动nginx)

五、Nginx基本配置

  1. 进入到conf文件夹,打开nginx.conf文件,可以看到一下内容

`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;
​
http {
    include       mime.types;
    default_type  application/octet-stream;
    server {
        listen       80; // 服务侦听80端口
        server_name  localhost; // 定义使用localhost来访问
​
        #charset koi8-r;
​
        #access_log  logs/host.access.log  main;
​
        root   D:/fordearme-box/fordearmebox-web/dist; // 定义服务器的默认网站根目录位置
        index  index.html index.htm; // 定义首页索引文件的名称
​
        // 当使用history模式的时候需要配置
        location / {   
            try_files $uri $uri/ /index.html;
        }
​
        #location / {
​
        #}
​
        location ~ ^/box/wechat/config { // 转发地址
            proxy_pass http://39.108.138.254:8082;
        }
​
        location ~ ^/box/ { // 转发地址
            proxy_pass http://39.108.138.254:8081;
        }
​
        #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;
        #}
    }
  }

 

  1. 例如后端部署在阿里云服务器上,阿里云的地址是39.108.138.254,悦心盒子接口的端口是8081,当我本地127.0.0.1访问悦心盒子接口时,就需要配置nginx的配置文件;由于接口的前缀都是“/box”开头,那我们可以在配置文件中写入正则,当接口请求到“/box/xxx"的时候就会转发到39.108.138.254的8081端口。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值