worker_processes auto;

events {
    worker_connections 1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout 65;

    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;

    # Gzip
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml text/javascript;
    gzip_min_length 1024;

    # 速率限制
    limit_req_zone $binary_remote_addr zone=api_limit:10m rate=30r/s;

    # ===== 生产环境 Upstream =====
    upstream prod_api {
        server rent-prod-server:3000;
    }
    upstream prod_merchant {
        server rent-prod-merchant:80;
    }
    upstream prod_platform {
        server rent-prod-platform:80;
    }
    upstream prod_website {
        server rent-prod-website:80;
    }

    # ===== 测试环境 Upstream =====
    upstream test_api {
        server rent-test-server:3000;
    }
    upstream test_merchant {
        server rent-test-merchant:80;
    }
    upstream test_platform {
        server rent-test-platform:80;
    }
    upstream test_website {
        server rent-test-website:80;
    }

    # ===== 生产环境 =====

    server {
        listen 80;
        server_name api.pinzhuhui.com;

        location / {
            limit_req zone=api_limit burst=50 nodelay;
            proxy_pass http://prod_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_set_header X-Forwarded-Proto $scheme;
            client_max_body_size 50m;
        }
    }

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

        location / {
            proxy_pass http://prod_website;
            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;
        }
    }

    server {
        listen 80;
        server_name merchant.pinzhuhui.com;

        location / {
            proxy_pass http://prod_merchant;
            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;
        }
    }

    server {
        listen 80;
        server_name platform-admin.pinzhuhui.com;

        location / {
            proxy_pass http://prod_platform;
            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;
        }
    }

    # ===== 测试环境 =====

    server {
        listen 80;
        server_name api-test.pinzhuhui.com;

        location / {
            limit_req zone=api_limit burst=50 nodelay;
            proxy_pass http://test_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_set_header X-Forwarded-Proto $scheme;
            client_max_body_size 50m;
        }
    }

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

        location / {
            proxy_pass http://test_website;
            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;
        }
    }

    server {
        listen 80;
        server_name merchant-test.pinzhuhui.com;

        location / {
            proxy_pass http://test_merchant;
            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;
        }
    }

    server {
        listen 80;
        server_name platform-admin-test.pinzhuhui.com;

        location / {
            proxy_pass http://test_platform;
            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;
        }
    }

    # 兜底：未知域名返回 444
    server {
        listen 80 default_server;
        return 444;
    }
}
