server {
    listen       80;
    listen       [::]:80;
    server_name  sunmi.551.com.tw;
    return 301 https://$host$request_uri;
}

server {
        listen 443 ssl;
        listen [::]:443 ssl;
	root /home/Sites/apps/sunmi/public/;
        index index.php index.html index.htm;
        ssl_certificate /etc/ssl/star.551.com.tw.crt;
        ssl_certificate_key /etc/ssl/star.551.com.tw.key;
        server_name sunmi.551.com.tw;
	access_log /var/log/nginx/sunmi.log;

        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

        location ~* ^/apps/(?<appid>[\d]+) {

                alias /home/Sites/apps/$appid/public;
                try_files $uri $uri/ @rewapp;

                location ~* ^/apps/(\d+)/(.*\.php)$ {
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_NAME $2;
                        fastcgi_param SCRIPT_FILENAME /home/Sites/apps/$1/public/$2;
                        fastcgi_pass php56:9000;
                        include fastcgi_params;
                }

                # Necessary for static files,
                # or may fall to another location with incorrect path
                # Add more extensions like PDF if necessary
                location ~* ^/apps/(\d+)/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt|woff|woff2))$ {
                        alias /home/Sites/apps/$1/public;
                        try_files $uri /$2 =404;
                }
        }

        location @rewapp {
                rewrite ^/apps/(\d+)/(.*)$ /apps/$1/index.php?/$2 last;
        }

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass php56:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

    	location /551backend {
        	auth_basic "Restricted Content";
         	auth_basic_user_file /etc/nginx/.htpasswd;
    	}

}
