티스토리 뷰

IT 이야기

Ubuntu에서 nginx 설치하기

하늘과 나b 2023. 11. 9. 22:30

1. Ubuntu에서 nginx 설치하기

 

Ubuntu에서 nginx 설치는 아래와 같이 할 수 있습니다.

sudo apt-get update
sudo apt-get install nginx

 

 

2. nginx 설정파일 작성하기

server {
    listen 80;
    server_name example.com; #nginx와 도메인 주소를 연결해 주는 역할을 합니다. 외부에서 example.com으로 들어오는 도메인 주소에서 요청이 들어오면 로컬에서 오픈되어 있는 아래의 " proxy_pass http://127.0.0.1:3000/; " 를 포워딩해줍니다.

    location / {
      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;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://127.0.0.1:3000/; # server_name example.com;로 들어온 요청을 nginx가 받아, 로컬에서 오픈되어 있는
"proxy_pass http://127.0.0.1:3000/;"를 포워딩해줍니다.
      proxy_redirect off;
    }

    gzip on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_min_length  1000;
    gzip_disable     "MSIE [1-6]\."
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
 }

 

위와 같이 입력한 후 CTRL + X 를 눌러 빠져나오고 Y를 눌러 파일을 저장합니다.

 

다음에는 /etc/nginx/sites-available에 만든 nginx설정 파일을 /etc/nginx/sites-enabled/ 직하로 옮겨주는 작업을 합니다.

 

cd /etc/nginx/sites-enabled/ 
ln -s /etc/nginx/sites-available/example example

 

이렇게 해주게 되면 방금 만든 Nginx 설정파일이 적용됩니다. 

설정을 변경한 이후에는 Nginx를 반드시 재시작 해줘야 변경한 내용이 적용됩니다.

 

 

Nginx 재시작 명령어

sudo /etc/init.d/nginx reload
혹은
sudo service nginx restart

 

 

 

댓글