nginx使用

lishihuan大约 2 分钟

nginx使用

用于记录一些使用上的一些问题及其处理

Nginx Location 匹配优先级

Nginx的location匹配有严格的优先级顺序:

  1. = 精确匹配 - 最高优先级
  2. ^~ 前缀匹配 - 第二优先级,匹配后不再检查正则
  3. ~~* 正则匹配 - 第三优先级,按配置顺序匹配
  4. 普通前缀匹配 - 最低优先级

实际示例

假设有这样的配置顺序:

location /tdtwx/ { ... }           # 优先级:4
location ~* \.png$ { ... }         # 优先级:3
location ^~ /tdtwx/ { ... }        # 优先级:2
location = /tdtwx/1/1/0.png { ... } # 优先级:1

对于请求 /tdtwx/1/1/0.png

  • 如果有 = 精确匹配,使用它
  • 否则如果有 ^~ 匹配,使用它并停止
  • 否则检查所有正则匹配
  • 最后使用最长的普通前缀匹配
# 普通前缀匹配 - 优先级低
location /tdtwx/ {
    alias E:/map/tdt_weixin/;
}

# 正则匹配 - 优先级高
location ~* \.(png|jpg|...)$ {
    # 没有root/alias,使用默认html目录
}

当请求 /tdtwx/1/1/0.png 时:

  1. 先匹配到 /tdtwx/ (普通前缀)
  2. 但nginx继续检查正则表达式
  3. 发现 ~* \.(png)$ 也匹配
  4. 正则匹配优先级更高,所以使用正则规则
  5. 正则规则没有指定路径,使用默认html目录
  6. 结果:在 html/tdtwx/1/1/0.png 找文件 ❌

nginx配置图片缓存

nginx_测试图片是否缓存.html

# nginx.conf 完整配置文件

# 工作进程数,通常设置为CPU核心数
worker_processes auto;

# 错误日志
error_log logs/error.log;

# 进程ID文件
pid logs/nginx.pid;

events {
    # 每个工作进程的最大连接数
    worker_connections 1024;
}

http {
    # MIME类型配置
    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;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    # Gzip压缩
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_comp_level 6;
    gzip_types
        text/plain
        text/css
        text/xml
        text/javascript
        application/javascript
        application/xml+rss
        application/json
        image/svg+xml;


    # 前端应用服务器配置
    server {
        listen 80;
        server_name localhost;  # 可以改为您的域名

        # 前端静态文件根目录(Vue打包后的dist目录)
        root html/dist;
        index index.html;

        # 前端路由支持(Vue Router history模式)
        location / {
            try_files $uri $uri/ /index.html;
        }

        # 后端API代理
        location /prod-api/ {
            proxy_pass http://127.0.0.1:8080/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

        # WMTS服务代理(矢量图)
        location /wmts/ {
            proxy_pass http://localhost:18080/geoserver/gwc/service/wmts/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

		# 天地图卫星图
		location /tdtwx/ {
			alias E:/map/tdt_weixin/;
			autoindex on;
			expires 7d;
			add_header Cache-Control "public, immutable";
			
			if ($request_method = 'OPTIONS') {
				return 204;
			}
		}

        # 错误页面
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }

    # 瓦片服务器配置(如果需要单独的瓦片服务)
    server {
        listen 18081;
        server_name localhost;

        # 瓦片文件根目录
        root html/tiles;
        
        # 瓦片文件访问
        location /tdtwx/ {
            alias html/tiles/tdtwx/;
            expires 30d;
            add_header Cache-Control "public, immutable";
            
            # 允许跨域访问
            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';
        }
    }
}