
# user nobody;
worker_processes	1;
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events { worker_connections  1024; }
http {
    include       		mime.types;
    default_type  		application/octet-stream;
	sendfile        	on;
    keepalive_timeout	65;
	upstream my_app {
		server 127.0.0.1:3000;         # 현재 실행 중인 버전
	}
	
	# log_format  main	'$remote_addr - $remote_user [$time_local] "$request" '
    #                  	'$status $body_bytes_sent "$http_referer" '
    #                  	'"$http_user_agent" "$http_x_forwarded_for"';
    # error_log  		logs/error.log  main;
    # tcp_nopush     	on;
    # gzip  			on;

    server {
		listen 8089;
		listen [::]:8089;
		server_name 127.0.0.1;

		location / {
			root D:/sourceCode/recent_ucb_front/.next/static/;
			index index.html;
			try_files $uri $uri/ /index.html index.htm;

			# 이미지 프록시
			location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|woff2)$ {
				proxy_pass http://localhost:8089;
			}

			# js파일 프록시
			# location ~* \.(?:js)$ {
			# 	proxy_pass http://localhost:3000;
			# }

			# cors 해제
			if ($request_method = 'OPTIONS') {
				add_header 'Access-Control-Allow-Origin' 'http://localhost:8089';
				add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
				add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
				add_header 'Access-Control-Max-Age' 1728000;
				add_header 'Content-Type' 'text/plain; charset=utf-8';
				add_header 'Content-Length' 0;
				return 204;
			}
			
			if ($request_method = 'POST') {
				add_header 'Access-Control-Allow-Origin' 'http://localhost:8089';
				add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
				add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
				add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
			}
			
			if ($request_method = 'GET') {
				add_header 'Access-Control-Allow-Origin' 'http://localhost:8089';
				add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
				add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
				add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
			}

			proxy_pass					http://my_app;
			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_redirect 				off;
		}

		# api route 프록시
		#location /api {
		#	proxy_pass http://localhost:8081/api/;
		#	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;
		#}
	}
	
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
