Nginx deploys front-end code to achieve front-end and back-end separation

  To achieve the separation of front and back ends, the front and back ends can be independently developed, deployed, and tested independently, and the two parties can exchange data through JSON.

  For front-end developers, there is no need to start or configure the Java/Tomcat runtime environment every time debugging; for back-end developers, there is no need to inject data into JSP pages.

  Deploying front-end code through nginx can help the front-end achieve the following basic requirements:

1. Request forwarding to solve the problem of cross-domain requests

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

  The above is a nginx configuration reference:

  listen nginx service port number

  location / Set the local code path accessed by the default root directory, here you can also set the default home page index

  proxy_pass request forwarding, you can configure multiple, match from top to bottom

  Take the first configuration as an example, that is, all local requests starting with /api/v1 will be forwarded to the corresponding online server, for example: http://localhost:7777/api/v1/getConfig will be automatically forwarded to: http: //api.yourhost.com/api/v1/getConfig

  In addition, it should also be noted that if the path configured by proxy_pass ends with /, as in the configuration v1 above, then the path is a relative path at this time, otherwise it is an absolute path

  For example, the forwarding configuration of v2: if the request is http://localhost:7777/api/v2/user/list, it will be forwarded from http://api.yourhost.com/new/user/list, without the original path /api/v2

2. gzip request compression

  The website enables gzip compression, which not only saves bandwidth, but also responds quickly to user visits.

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

  The following are the functions of each configuration:

  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; (什么类型的页面或文档启用压缩)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324653742&siteId=291194637