server {
    listen 80;
    server_name staging.spottorg.ru;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl ;
    server_name staging.spottorg.ru;

    ssl_certificate /etc/nginx/ssl/spottorg.crt;
    ssl_certificate_key /etc/nginx/ssl/spottorg.key;

    root /application/public;

    client_max_body_size 30m;
    client_body_buffer_size 30M;

    location / {
        access_log /var/log/nginx/landing_access.log json;
        error_log /var/log/nginx/landing_error.log;
        proxy_pass http://landing;
        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;
        chunked_transfer_encoding off;
        proxy_buffering off;
        proxy_cache off;
    }

    location /socket.io/auctionreload {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_pass http://php:8080/;
    }

    location @rewriteapp {
        rewrite ^(.*)$ /index.php/$1 last;
    }

    location /logout {
        try_files $uri @rewriteapp;
    }
    
    location /api {
        rewrite ^(.*)$ /index.php/$1 last;
    }

    location /clockwork {
        rewrite ^(.*)$ /index.php/$1 last;
    }

    location /__clockwork {
        rewrite ^(.*)$ /index.php/$1 last;
    }

    location /pdf {
        rewrite ^(.*)$ /index.php/$1 last;
    }

    location /1c {
        rewrite ^(.*)$ /index.php/$1 last;
    }

    location /socket {
        try_files $uri @rewriteapp;
    }

    location /admin {
        try_files $uri @rewriteapp;
    }

    location ~* \/admin\/.*(js|jpg|png|css|woff|woff2)$ {
        proxy_pass http://static;
    }

    location /media {
        try_files $uri @rewriteapp;
    }

    location /img {
        proxy_pass http://static;
        # alias /application/public/img;
        # try_files $uri /index.php$request_uri;
        # expires 6M;
    }

    location ~ ^/index\.php(/|$) {
        access_log /var/log/nginx/backend_access.log json;
        error_log /var/log/nginx/backend_error.log;
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' $http_origin;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Expeditor-Domain,Authorization';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }

        #add_header 'Access-Control-Allow-Origin' $http_origin;
        add_header 'Access-Control-Allow-Origin' *;
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Expeditor-Domain,Authorization';

        fastcgi_pass backend:9001;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # location /mysql_exporter {
    #     access_log /var/log/nginx/mysql_exporter_access.log json;
    #     error_log /var/log/nginx/mysql_exporter_error.log;

    #     #if ($allow = 0) {
    #     #    return 403;
    #     #}
    #     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #     proxy_set_header X-Scheme $scheme;
    #     proxy_set_header Host $http_host;
    #     proxy_redirect off;
    #     proxy_http_version 1.1;
    #     proxy_pass http://mysqld-exporter:9104/metrics;
    # }
    # location /redis_exporter {
    #     access_log /var/log/nginx/mysql_exporter_access.log json;
    #     error_log /var/log/nginx/mysql_exporter_error.log;
    #     #if ($allow = 0) {
    #     #    return 403;
    #     #}
    #     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #     proxy_set_header X-Scheme $scheme;
    #     proxy_set_header Host $http_host;
    #     proxy_redirect off;
    #     proxy_http_version 1.1;
    #     proxy_pass http://redis-exporter:9121/metrics;
    # }
}
