Engineering Note

[Nginx] 리눅스 서버에 Nginx 설치 및 설정 과정 본문

DevOps

[Nginx] 리눅스 서버에 Nginx 설치 및 설정 과정

Software Engineer Kim 2025. 9. 20. 16:39

프로젝트를 하면서 리눅스에 Nginx를 설치하고 Nginx 가 필요한 이유를 정리하려고 한다.

Nginx

- Nginx는 HTTP요청에 대한 응답을 처리하는 Webserver, Reverse Proxy, content cache, load balancer, TCP/UDP proxy server 로 역할이 가능한 Software

 

Nginx 설치  

 

 

Nginx를 설정 파일

- nginx가 어떤 포트 번호를 사용할 지, 어디로 리다이렉트 할 지, 캐시 지속 시간은 어떻게 할 지 등등에 대해서 설정할 수 설정파일 위치에 아래 디렉터리 아래에 default 라는 파일로 있다.

/etc/nginx/sites-available

 

 

 

vi 에디터로 열어보면 아래 처럼 초기 설정되어 있다.

        #
        # 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 _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/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;
        #}
}


# 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;
#       }
#}

 

 

초기 설정 파일은 백업을 해두고 프로젝트에 적용할 파일을 만들어 주었다.

spring-app 이라는 파일로 '/etc/nginx/sites-available' 디렉터리에 만들어 주고 

 

nginx 디렉토리 구조의 차이

- /etc/nginx/sites-available : 실제 활성화된 파일은 아니고 여러 설정파일을 보관하는 디렉터리

- /etc/nginx/sites-enabled/ : 실제 활성화할 설정 파일을 저장하는 디렉터리

 

 

윈도우의 바로가기 처럼 심볼링 링크로 활성화 디렉터리 '/etc/nginx/sites-enabled/' 여기 있는 파일은 설정 파일이라는 설정은 

'/etc/nginx/nginx.conf' 여기에 적혀 있다. 그래서 site-enabled에 설정파일을 두면 nginx가 실행할 때 알아서 설정파일을 적용한다.

 

 

명령어 정리

 

Nginx 설치 

 

#패키지 업데이트
sudo apt update

#Nginx 설치
sudo apt install nginx -y

 

 

1.2 설정 파일 (/etc/nginx/sites-available/spring-app)

server {
 listen 80;
 server_name domain name;

 location / {
  proxy_pass http://127.0.0.1:8080;
  proxy_set_header Host $host;
  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;
 }

}

 

- domain name에 서버 도메인 또는 IP 주소를 적어주면 된다.

그러면 모든 요청('/')에 대해 127.0.0.1:8080 으로(localhost:8080)으로 라우팅을 한다.

 

이렇게 되면, 아래 요청에 대해 모두 proxy_pass 경로로 라우팅한다.

http://domain/                    ✅
http://domain/home               ✅  
http://domain/api/users          ✅
http://domain/static/style.css   ✅
http://domain/admin/dashboard    ✅
http://domain/a/b/c/d/e         ✅

 

 

 

 

 

1.3 설정 파일 심볼릭 링크를 통해 활성화

 

sudo ln -s /etc/nginx/sites-available/spring-app /etc/nginx/sites-enabled/

 

 

 

 

Comments