server {
    listen 80;
    server_name ebook.miduway.space;
    
    # Редирект всех HTTP-запросов на HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name ebook.miduway.space;

    # SSL-сертификаты
    ssl_certificate /etc/letsencrypt/live/ebook.miduway.space/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/ebook.miduway.space/privkey.pem;
    
    # Настройки SSL
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    # Проксирование к Docker-контейнеру
    location / {
        proxy_pass http://localhost:80;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        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;

        # Настройки для Vue.js
        try_files $uri $uri/ /index.html;
        add_header Cache-Control "no-cache, no-store, must-revalidate";
    }

    # Кэширование статических файлов Vue
    location /js/ {
        proxy_pass http://localhost:80;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        
        # Настройки кэширования для JS файлов
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    location /css/ {
        proxy_pass http://localhost:80;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        
        # Настройки кэширования для CSS файлов
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    location /img/ {
        proxy_pass http://localhost:80;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        
        # Настройки кэширования для изображений
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    # Блокировка доступа к скрытым файлам
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    # Обработка ошибок для Vue.js
    error_page 404 /index.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
} 