nginx部署前端代码 负载均衡

用户在前端页面点击请求, 到nginx(根据upstream配置的后端服务器 ip:端口 和 location匹配规则   负载到后端)。

nginx部署前端代码 负载均衡_第1张图片

nginx部署前端代码 负载均衡_第2张图片

nginx部署前端代码 负载均衡_第3张图片

http://nginx.org/en/download.html

nginx部署前端代码 负载均衡_第4张图片

 

nginx开源版 与 nginx plus 商用版本

nginx部署前端代码 负载均衡_第5张图片

 

下载windows版的 nginx-1.20.0.zip , 查看 D:\nginx-1.20.0\conf\nginx.conf   配置文件

nginx部署前端代码 负载均衡_第6张图片

#server表示虚拟主机可以理解为一个站点,可以配置多个server节点搭建多个站点
#每一个请求进来确定使用哪个server由server_name确定

 

#location用来匹配同一域名下多个URI的访问规则
        #比如动态资源如何跳转,静态资源如何跳转等
        #location后面跟着的/代表匹配规则
        location / {
            #站点根目录,可以是相对路径,也可以使绝对路径
            root   html;
            #默认主页
            index  index.html index.htm;
            
            #转发后端站点地址,一般用于做软负载,轮询后端服务器
            #proxy_pass http://10.11.12.237:8080;

#指定用户
user  root;
#工作线程程数   核心数的2倍
worker_processes  8;
#日志记录
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
# 进程id保存的位置, 都会自动生成
#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


#http模块
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;
    
    #这一块 负载均衡  需要我们自己添加
    upstream server_group {
        server 127.0.0.1:8050    weight=1  max_fails=1  fail_timeout=20;
        server 127.0.0.1:8060    weight=1;
    }

    server {
        #server模块, 监听的端口  localhost:80 访问
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        #所有的请求都会进入location中,支持正则表达式, 
        location / {
            root   html;  #表示root用户权限寻找html相对路径下的页面
            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;
        #}
    }


    # 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;
    #    }
    #}

}

 

location /{
            root  /var/www/html/yanwei/private-arbitrate-client  就是前端代码存放路径

            index index.html index.htm;
}

location /api/v1    location /api/v2   以/api/v1开头的请求 或    以/api/v2开头的请求   都会转发到后台   即前端到后端的跳转url被nginx拦截再转发。

实现前后端分离,可以让前后端独立开发、独立部署、独立单测,双方通过JSON进行数据交互。

  对于前端开发人员来说,不用每次调试都需要启动或配置Java/Tomcat运行环境;

       对于后端开发人员来说 ,也不用在需要往JSP页面注入数据。

  通过nginx来部署前端代码,可以帮助前端实现以下基本需求:

1、请求转发,解决请求跨域的问题       使用nginx做代理来实现跨域和负债均衡

server {
        listen      7777;
        location /{
            root  /Users/xiaoyun/git/someproject/dist;
        }

        location /api/v1{
              proxy_set_header Host api.yourhost.com;
              proxy_pass http://api.yourhost.com/api/v1/;
        }

        location /api/v2{
                proxy_pass  http://api.yourhost.com/new;
        }
    }

  以上是一段nginx配置参考:

  listen    nginx服务端口号

  location  /    设置默认根目录所访问的本地代码路径,这里还可以设置默认主页index

  proxy_pass  请求转发,可以配置多个,从上至下进行匹配

  以第一个配置为例,即所有本地以/api/v1开头的请求都会转发至对应线上服务器,例如: http://localhost:7777/api/v1/getConfig 则会自动转发到:http://api.yourhost.com/api/v1/getConfig

  另外,还需要注意的是,proxy_pass配置的路径如果是以/结尾,如上面的配置v1,那么此时路径为相对路径,否则为绝对路径

  例如 v2的转发配置:如果请求 http://localhost:7777/api/v2/user/list  则会转发自 http://api.yourhost.com/new/user/list ,不会带有原路径的 /api/v2

2、gzip请求压缩

  网站开启gzip压缩,不仅能够节省带宽,也能够快速响应用户的访问

 

http{
    gzip  on;
    gzip_proxied any;
    gzip_min_length  1024;
    gzip_buffers    4 8k;
    gzip_comp_level 3;
    gzip_types      text/plain text/css application/x-javascript application/javascript application/xml application/json;
}

  以下为各项配置作用:

  gzip on;      (启用 gzip 压缩功能)

  gzip_proxied any;  (nginx 做前端代理时启用该选项,表示无论后端服务器的headers头返回什么信息,都无条件启用压缩)

  gzip_min_length  1024; (最小压缩的页面,如果页面过于小,可能会越压越大,这里规定大于1K的页面才启用压缩)

  gzip_buffers    4 8k; (设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流)

  gzip_comp_level 3; (压缩级别,1压缩比最小处理速度最快,9压缩比最大但处理最慢,同时也最消耗CPU,一般设置为3就可以了)

  gzip_types      text/plain text/css application/x-javascript application/javascript application/xml application/json; (什么类型的页面或文档启用压缩)

你可能感兴趣的:(工具使用)