GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — blog/posts/9.md at master · RetroAstro/blog

Expand file tree

/

9.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
303 lines (228 loc) · 7.86 KB
/

9.md

Copy path

File metadata and controls

303 lines (228 loc) · 7.86 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

简介:

Nginx 是一个异步框架的 Web 服务器, 通常被用作反向代理、负载均衡器和 HTTP 缓存等。

特点:

  • 单次请求响应更快,并且在高并发环境下比其他 Web 服务器有更快的响应。
  • 基于模块化设计,由多个耦合度极低的模块组成,具有很高的扩展性。
  • 可靠性高,每个 worker 进程相对独立,master 进程在一个 worker 进程出错时可以快速拉起新的 worker 子进程提供服务。
  • 消耗内存低,一般情况下,10000个非活跃的 HTTP Keep-Alive 连接在 Nginx 中仅消耗 2.5MB 的内存,这是 Nginx 支持高并发连接的基础。单机支持10万以上的并发连接。
  • 提供热部署功能,即在 7x24 小时不间断服务的前提下,升级 Nginx 的可执行文件。当然,它也支持不停止服务就更新配置项,更换日志文件等功能。

常用参数

  1. 正则表达式匹配
~ 为区分大小写匹配
~* 为不区分大小写匹配
!~ 和 !~* 分别为区分大小写不匹配及不区分大小写不匹配
-f 和 !-f 用来判断是否存在文件
-d 和 !-d 用来判断是否存在目录
-e 和 !-e 用来判断是否存在文件或目录
-x 和 !-x 用来判断文件是否可执行
  1. 变量 详细查看

示例:

路由的匹配和错误文件的配置

server {
    listen 80;
    server_name test.me;
    
    # 配置访问 /test.js 时报 403 错
    location /test.js {
        return 403;
    }

    # 配置访问 /404 时报 404 错
    location /404 {
        return 404;
    }
    
    # 用if匹配以 /501/ 开头的,匹配 /501/1,/501/1/2 但不匹配 /501
    if ($request_uri ~* ^/501/) {
        return 501;
    }
    
    # 用location匹配 /502/ /502 /502/1 /502/1/2
    location ~* "^/502(/.*)?$" {
        return 502;
    }
    
    # 注意 error_page 配置时加 = 和不加 = 的区别
    # 加了 = 表示响应为指定的 http status code,默认为200,不加 = 为原错误的状态码
    
    # 这样配置访问错误页面时 http status 为 200 ,但页面内容是 404.html 的内容
    error_page 404 500 = /404.html;
    
    # 这样配置访问错误页面时 http status 为 404 ,但页面内容是 404.html 的内容
    error_page 404 500 =404 /404.html;

    # 也可以把404请求直接301到某个域上
    error_page 404 =301 https://test.com/404;
}

url 重定向的配置

rewrite 指令:可以使用在 server, location, if 区域

语法:rewrite regex replacement flag

尾部的标记 ( flag )

  • last - 完成重写的指令,之后搜索相应的 uri 或 location
  • break - 完成重写指令
  • redirect - 返回 302 临时重定向
  • permanent - 返回 301 永久重定向
server {
    # 设置多个域名
    server_name www.test.com test.com
    # permanent 表示301重定向
    if ( $host != 'test.com' ) {
        rewrite ^/(.*)$ http://test.com/$1 permanent;
    }
}

注意事项:

如果在 location 中执行了 rewrite 指令产生了新的 URI,那么 location 又一次确定了新的URI。这样的循环可以最多执行10次,超过以后 nginx 将返回 500 错误 。last 与 break 的区别在于,last 并不会停止对下面 location 的匹配,因此为了避免这种情况应该把 flag 设置为 break。

location /test1/ {
  rewrite ^(/test1) $1/test2/index.html break;
  return   403;
}

将 /photos/123456 重写为 /path/to/photos/12/1234/123456.png

rewrite  "/photos/([0-9] {2})([0-9] {2})([0-9] {2})" /path/to/photos/$1/$1$2/$1$2$3.png;

nodejs 反向代理

server {
    server_name www.test.com;
    listen 80;

    # 设置这个网站的根目录
    root /www/www.test.com/;

    # 由于下面配置了文件不存在则代码到 Node.js 中,那么直接访问目录(不带默认主页)的话会有问题
    # 如果访问目录下有 index.html 文件,则直接重写到该文件
    # break 表示重写且停止,但 url 不变,而 permanent 表示301重定向,url 会更新
    if ( -f $request_filename/index.html ){
        rewrite (.*) $1/index.html break;
    }

    # 如果请求的文件不存在,则代理到 Node.js
    if ( !-f $request_filename ){
        rewrite (.*) /index.js;
    }

    # 代理node服务 8001
    location = /index.js {
        # 设置一些代理的header信息,这些信息将被透传到 Node.js 服务的header信息里
        proxy_set_header Connection "";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        # 代理服务
        proxy_pass http://127.0.0.1:8001/$request_uri;

        # 忽略其他重写
        proxy_redirect off;
    }
}

配置图片防盗链

防盗链是指当图片不是自己网站打开时返回 403 或者指定图片,是通过请求的来路判断是否是自己的站点来设置响应。

语法:

valid_referers none | blocked | server_names

  • none 没有来路
  • blocked 有来路
  • server_names 来路里包含当前域名
server {
    # 配置所有图片
    location ~* \.(gif|jpg|png|bmp)$ {
        # 验证可以是有来路且来路匹配 test.com、或者匹配当前域名
        valid_referers blocked *.test.com server_names;

        # 如果验证不通过则返回403
        if ($invalid_referer) {
            return 403;
        }
    }
}

配置 HTTPS

# 配置个http的站点,用来做重定向,当然如果你不需要把 HTTP->HTTPS 可以把这个配置删了
server {
    listen       80;

    # 配置域名
    server_name www.test.com test.com;

    # 配置让这些 HTTP 的访问全部 301 重定向到 HTTPS 的
    rewrite ^(.*) https://www.test.com/$1 permanent;
}

# 配置 HTTPS
server {
    # 配置域名
    server_name www.test.com test.com;

    # https默认端口
    listen 443;

    # https配置
    ssl on;
    ssl_certificate /test/www.test.com.crt;
    ssl_certificate_key /test/www.test.com.key;
}

使用 nginx-http-concat 来合并 HTTP 请求

# 下载
wget https://github.com/alibaba/nginx-http-concat/archive/1.2.2.tar.gz

# 解压并记录解压后的目录
tar xzf 1.2.2.tar.gz
# 配置
./configure 其他编译参数 --add-module=/刚才解压的目录
# 安装
make
# 如果是重新编译安装时则不要运行 make install
[sudo] make install
# 使用 location 匹配到你想要匹配的路径
server {
    location /static/css/ {
        concat on;
        concat_types text/css;
        concat_max_files 20;
    }
        
    location /static/js/ {
        concat on;
        concat_types application/javascript;
        concat_max_files 30;
    }
}

使用 nginx -s reload 重启 nginx

现在请求包含 /static/css/??a.css,b.css 的 url 时就可以看到返回 a.css、b.css 合并内容

配置浏览器缓存

单位

ms  milliseconds
s   seconds
m   minutes
h   hours
d   days
w   weeks
M   months,30 days
y   years,365 days

常规配置

server {
    # 设置为1月
    set $expires_time           1M;

    # 针对后台不缓存
    if ($request_uri ~* ^/admin(\/.*)?$) {
        # 输出 Cache-Control:no-cache
        set $expires_time       -1;
       
    }

    # 针对静态文件缓存最大
    if ($request_uri ~* ^/static(\/.*)?$) {
        # 输出 Cache-Control:max-age=315360000
        set $expires_time       max;
    }
    
    expires $expires_time;
}

配置默认主页

server {
    # 网站根目录
    root /home/project/;

    # 设置默认主页,支持多个,按优先级来,空格分格
    index index.html index.htm index.php;
}

参考内容: Nginx 入门教程

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.