/etc/nginx/sites-available/default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name j9b205.p.ssafy.io;
location /api {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
proxy_pass http://localhost:9090;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
location /ai {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
proxy_pass http://localhost:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/j9b205.p.ssafy.io/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/j9b205.p.ssafy.io/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
server {
if ($host = j9b205.p.ssafy.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name j9b205.p.ssafy.io;
return 404; # managed by Certbot
}
자 일단 이 nginx 설정파일을 한번 뜯어보자.
크게 2개의 서버 블록이 있다.
일단 밑에 있는 작은 server 블록부터 보자
server {
if ($host = j9b205.p.ssafy.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name j9b205.p.ssafy.io;
return 404; # managed by Certbot
}
이 코드를 일목요연하게 설명해보자.
일단 이 서버 블록은 80포트의 요청을 받는다. 근데 80포트는 뭐다? http 요청!!
그래서 이 서버 블록이 의미하는 건
listen 80 default_server;
listen [::]:80 default_server;
server_name j9b205.p.ssafy.io;
우리는 일단 80(http)포트로 오는 요청을 받을꺼야.
그리고 server_name은 j9b205.p.ssafy.io 이거고.
근데 만약에 그 요청을 보낸 host가 j9b205.p.ssafy.io 면??
if ($host = j9b205.p.ssafy.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
Nginx : “너가 보낸 요청 가지고 그대로 301 리턴할께~ “
근데 301은?? → 리다이렉트 요청!!
말 그대로 “j9b205.p.ssafy.io 로 들어온 http 요청은 https로 바꿔서 그대로 리다이렉트할께!” 를 의미한다.
그러면? 그 다음엔? 다시 리다이렉트했으면 요청이 들어갔겠죠??
어디로??
바로 위에 있는 서버 블록으로!!
왜냐?
server {
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name j9b205.p.ssafy.io;
location /api {
proxy_pass http://localhost:9090;
}
location /ai {
proxy_pass http://localhost:8000;
}
location / {
proxy_pass http://localhost:3000;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/j9b205.p.ssafy.io/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/j9b205.p.ssafy.io/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
(가독성을 위해 주석처리된 코드는 지웠슴다)
이 코드를 보면 밑에
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
이렇게 443(https)포트로 들어오는 요청은 이 서버 블록이 받는다고 나와있으니까!!
그럼 이제 이 서버 블록이 하는 다른 일은 뭘까요
첫번째는 바로
ssl_certificate /etc/letsencrypt/live/j9b205.p.ssafy.io/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/j9b205.p.ssafy.io/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
“letsencrypt에서 발급받는 키를 사용해 SSL 처리도 해줄께!!”
두번째는
location /api {
proxy_pass http://localhost:9090;
}
location /ai {
proxy_pass http://localhost:8000;
}
location / {
proxy_pass http://localhost:3000;
}
이 세가지 location 설정이다.
여기서 각각의 location의 의미는
이런 의미이다.