Nginx笔记

配置参考

# 80 转 443
server {
    listen       80;
    server_name  www.trycatch.xyz;
    rewrite ^(.*) https://$server_name$1 permanent;
}

server {
    listen       443 ssl;
    server_name  www.trycatch.xyz;

    # gzip
    gzip             on;
    gzip_types       *;
    gzip_comp_level  6;

    # auth
    auth_basic            "auth";
    auth_basic_user_file  /path/to/auth_file;

    # ssl
    ssl_certificate       /path/to/trycatch.xyz.cert.pem;
    ssl_certificate_key   /path/to/trycatch.xyz.key.pem;

    # 反向代理
    location / {
        proxy_pass   http://127.0.0.1:8080;
    }

    # 反向代理 /api/abc => /api/abc
    location /api/ {
        proxy_pass   http://127.0.0.1:8080;
    }

    # 反向代理 /api/abc => /abc
    location /api/ {
        proxy_pass   http://127.0.0.1:8080/;
    }

    # websocket
    location /ws {
        proxy_pass  http://server:8081;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    # vue配置
    location / {
        root   /home/dist/;
        try_files $uri $uri/ /index.html;
        index index.html;
    }
}

# minio

server {
    listen 80;
    server_name  minio.xyz;

    # gzip
    gzip             on;
    gzip_types       *;
    gzip_comp_level  6;

    client_max_body_size 0;
    ignore_invalid_headers off;
    chunked_transfer_encoding off;

    location / {
        proxy_pass   http://minio:9000;
        proxy_set_header Host $host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

server {
    listen 80;
    server_name  console.minio.xyz;

    # gzip
    gzip             on;
    gzip_types       *;
    gzip_comp_level  6;

    client_max_body_size 0;
    ignore_invalid_headers off;
    chunked_transfer_encoding off;

    location / {
        proxy_pass   http://minio:9001;
        proxy_set_header Host $host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

查找的顺序及优先级

  • 精确匹配 =
  • 前缀匹配 ^~(立刻停止后续的正则搜索)
  • 按文件中顺序的正则匹配 ~(区分大小写)或~*(不区分大小写)
  • 匹配不带任何修饰的前缀匹配。

Leave a Comment

Back to Top