load_module /etc/nginx/modules/ngx_http_image_filter_module.so;
user root;
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    client_max_body_size 2048m;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            # 禁用所有缓存（关键！）
            add_header Cache-Control "no-cache, no-store, must-revalidate" always;
            add_header Pragma "no-cache" always;
            add_header Expires "0" always;

            root   /usr/share/nginx/html/dist;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

         location /api/ {
            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_pass http://dj-multictrl-api:8080/api/;
         }

        location /geo/model {
            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 /data;
        }

        location ~* /video/(.+)$ {
            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';
            alias /data/$1;
        }

        location ~* /image/(.+)$ {
            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';

            # 图片服务器端存储地址
            alias /data/$1;
            # 图片默认宽度
            set $width -;
            # 图片默认高度
            set $height -;
            if ($arg_width != "" ) {
                set $width $arg_width;
            }
            if ($arg_height != "" ) {
                set $height $arg_height;
            }
            # 设置图片宽高
            image_filter resize $width $height;
            # 设置nginx读取图片最大buffer
            image_filter_buffer 50M;
            # 是否开启图片隔行扫描
            image_filter_interlace on;
            error_page 404 = error.gif;
        }

        location /rtc/v1/whep/ {
            proxy_pass http://dj-multictrl-srs:61630/rtc/v1/whep/;
        }

        location /flv/ {
            proxy_pass http://dj-multictrl-srs:61631/;
        }

        location /mqtt {
            proxy_pass http://dj-multictrl-emqx:8083;
            # WebSocket 必需的头设置
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            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;
            add_header Access-Control-Allow-Origin *;
             #       add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
            add_header Access-Control-Allow-Headers "Content-Type, Authorization";
            # 超时设置（对于长连接的 MQTT 很重要）
            proxy_read_timeout 3600s;
            proxy_send_timeout 3600s;
            proxy_connect_timeout 30s;
            # 禁用缓存
            proxy_buffering off;
        }

        location /mqtt2 {
                proxy_pass http://dj-multictrl-emqx2:8083;
                # WebSocket 必需的头设置
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                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;
                add_header Access-Control-Allow-Origin *;
                 #       add_header Access-Control-Allow-Origin *;
                add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
                add_header Access-Control-Allow-Headers "Content-Type, Authorization";
                # 超时设置（对于长连接的 MQTT 很重要）
                proxy_read_timeout 3600s;
                proxy_send_timeout 3600s;
                proxy_connect_timeout 30s;
                # 禁用缓存
                proxy_buffering off;
        }

        location ~* ^/file/(.+?\.(xlsx|xls|docx|doc|kmz|tar))$ {
            alias /data/$1;
            # 强制浏览器下载文件
            add_header Content-Disposition "attachment";
            add_header Content-Type application/octet-stream;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    include conf.d/*.conf;
}
