nginx 설치하기

By | 2021년 8월 25일
Table of Content

nginx 설치하기

docker 로 nginx 설치하기

docker run -d \
    -p 8080:80 \
    --name nginx \
    nginx

http://localhost:8080/ 으로 접속하면 nginx 가 작동하는 것을 확인할 수 있습니다.

nginx 에 https 적용하기

docker exec -it nginx cat /etc/nginx/nginx.conf
sudo mkdir -p /etc/nginx
sudo mkdir -p /etc/nginx/sites-available
sudo mkdir -p /etc/nginx/sites-enabled

아래와 같이 파일을 수정합니다.

    server {
        listen       8443 ssl http2 default_server;
        listen       [::]:8443 ssl http2 default_server;
        server_name    _;

        ssl_certificate       /ssl/fullchain.pem;
        ssl_certificate_key   /ssl/privkey.pem;

        root /usr/share/nginx/html;
        index index.html index.htm index.nginx-debian.html;
    }
sudo vi /etc/nginx/nginx.conf
--------------------------------------
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    server {
        listen       8443 ssl http2 default_server;
        listen       [::]:8443 ssl http2 default_server;
        server_name  _;

        ssl_certificate       /ssl/fullchain.pem;
        ssl_certificate_key   /ssl/privkey.pem;

        root /usr/share/nginx/html;
        index index.html index.htm index.nginx-debian.html;
    }

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
--------------------------------------
docker run -d \
    -p 8443:8443 \
    --name nginx \
    -v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf \
    -v /ssl/fullchain.pem:/ssl/fullchain.pem \
    -v /ssl/privkey.pem:/ssl/privkey.pem \
    nginx

도메인 추가

sudo vi /etc/nginx/sites-available/example.com
--------------------------------------
server {
    listen 80;
    listen [::]:80;

    root /var/www/example.com/html;
    index index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

    location / {
        try_files $uri $uri/ =404;
    }
}
--------------------------------------
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

proxy 추가

sudo vi /etc/nginx/sites-available/test.skyer9.pe.kr
--------------------------------------
server {
    listen 8443;
    listen [::]:8443;

    server_name test.skyer9.pe.kr;

    location / {
        proxy_set_header Host $host:$server_port;
        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;

        proxy_pass http://test.skyer9.pe.kr:9000;
    }
}
--------------------------------------
sudo ln -s /etc/nginx/sites-available/test.skyer9.pe.kr /etc/nginx/sites-enabled/
docker run -d \
    -p 8443:8443 \
    --name nginx \
    -v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf \
    -v /ssl/fullchain.pem:/ssl/fullchain.pem \
    -v /ssl/privkey.pem:/ssl/privkey.pem \
    -v /etc/nginx/sites-available:/etc/nginx/sites-available \
    -v /etc/nginx/sites-enabled:/etc/nginx/sites-enabled \
    nginx

답글 남기기