怎么配置好一个网站的nginx代理?nginx的概念和使用

463 阅读2分钟

nginx用途和配置实践

Nginx是什么?

“Nginx 是一款轻量级的 HTTP 服务器,采用事件驱动的异步非阻塞处理方式框架,这让其具有极好的 IO 性能,时常用于服务端的反向代理负载均衡。”

nginx的主要用途:动静分离,反向代理,正向代理,负载均衡。

动静分离

连前端都看得懂的《Nginx 入门指南》 - 掘金

配置反向代理

配置一个简单的反向代理是很容易的,代码如下:

server {  
        listen       8080;        
        server_name  localhost;

        location / {
            root   html; # Nginx默认值
            index  index.html index.htm;
        }
        
        proxy_pass http://localhost:8000; # 反向代理配置,请求会被转发到8000端口
}

反向代理的表现很简单。那上面的代码块来说,其实就是向nginx请求localhost:8080跟请求 http://localhost:8000 是一样的效果。(跟代购的原理一样)

正向代理和反向代理:cloud.tencent.com

正向代理

比如vpn:

负载均衡

# 负载均衡:设置domain
upstream domain {
    server localhost:8000;
    server localhost:8001;
}
server {  
        listen       8080;        
        server_name  localhost;

        location / {
            # root   html; # Nginx默认值
            # index  index.html index.htm;
            
            proxy_pass http://domain; # 负载均衡配置,请求会被平均分配到8000和8001端口
            proxy_set_header Host $host:$server_port;
        }
}

负载均衡的6种算法,Ngnix的5种算法!

安装

以mac为例:

Mac 安装Nginx详细教程 - 掘金

#安装
brew install nginx
#查看nginx的配置信息,如下命令
brew info nginx

默认静态资源位置:/opt/homebrew/var/www

默认nginx文件位置:/opt/homebrew/etc/nginx/nginx.conf

如何配置nginx?

(一)http > server > location

nginx配置结构分为三层 http > server > location http 包含一到多个server, server包含一到多个location 配置项的优先级分别是location, server, http

举例:

http {
        ...
        access_log /var/logs/nginx/nginx.log;
        
        server {
                server_name A;
                ...
                access_log /var/logs/nginx/serverA/nginx.log;
                
                location / {
                                ...
                                access_log /var/logs/nginx/serverA/localtion/nginx.log;
                }
        }
}

匹配到server A,localtion /时日志会记录到 /var/logs/nginx/serverA/localtion/nginx.log;

匹配到server A 其他location时日志会记录到/var/logs/nginx/serverA/nginx.log

默认请求日志记录到 /var/logs/nginx/nginx.log;

(二)location优先级说明

location = / {
    [ configuration A ]
}

location / {
    [ configuration B ]
}

location /documents/ {
    [ configuration C ]
}

location ^~ /images/ {
    [ configuration D ]
}

location ~* .(gif|jpg|jpeg)$ {
    [ configuration E ]
}
  1. The “/” request will match configuration A,

  2. the “/index.html” request will match configuration B,

  3. the “/documents/document.html” request will match configuration C,

  4. the “/images/1.gif” request will match configuration D,

  5. the “/documents/1.jpg” request will match configuration E.

  6. The “@” prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection. They cannot be nested, and cannot contain nested locations.

优先级规则

  1. (=)的优先级最高

  2. 匹配(^~)优先级次之。不支持正则表达式。如果有多个location匹配的话,则使用表达式最长的那个

  3. 正则表达式类型(~ ~*)的优先级次之

  4. 常规字符串匹配,如果有多个location匹配的话,则使用表达式最长的那个

小结

  • nginx配置分三层结构,层级越深配置优先级越高

  • location 是否以/结尾和当前访问的url影响着是否301跳转

  • proxy_pass是否以/结尾影响实际访问的url是否包含location配置的前缀

静态资源压缩

可以设置在http\server、location任意一级

gzip on;
    gzip_comp_level 5;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_proxied any;
    gzip_vary on;
    gzip_types
      application/javascript
      application/x-javascript
      text/javascript
      text/css
      text/xml
      application/xhtml+xml
      application/xml
      application/atom+xml
      application/rdf+xml
      application/rss+xml
      application/geo+json
      application/json
      application/ld+json
      application/manifest+json
      application/x-web-app-manifest+json
      image/svg+xml
      text/x-cross-domain-policy;
    gzip_static on;  
    gzip_disable "MSIE [1-6].";

缓存

location ~ .*.(?:js|css|gif|jpg|png|svg)$ {
        add_header Cache-Control max-age=31536000;
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
        root   /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
    }

参考:Nginx基本配置http,server,location三层结构说明_user www www;-CSDN博客