OKdevTV Docs
Home OKdevTV
ESC
nginx 2 min read 3.2 KB Raw Markdown
On this page (12)

nginx

nginx 설치

  • aws EC2 기준
sh
sudo dnf install nginx -y
# centos 7
sudo amazon-linux-extras install nginx1
sudo systemctl start nginx
  • 기본 설정
sh
curl -i http://localhost
sudo chmod 644 /var/log/nginx
sudo chown -R ec2-user:ec2-user /usr/share/nginx/html
echo "<h1>Hello World</h1>" > /usr/share/nginx/html/hello.html

자동 시작

sh
sudo systemctl enable nginx.service

설정

  • cd /etc/nginx/ && vim nginx.conf 기본 설정 파일
  • conf.d/ 폴더에 도메인별로 설정 파일 분리 가능

Reverse Proxy

  • “서버단에 다수의 웹서버를 두고, 매번 요청이 발생할 때마다 어떤 서버에게 이 요청을 처리하도록 지시할지 결정하는 역할을 수행한다.”
nginx
    location / {
        sendfile off;
        proxy_pass         http://127.0.0.1:3000;
        proxy_redirect     default;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade           $http_upgrade; # for websocket
        proxy_set_header   Connection        "upgrade"; # for websocket
        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;
        proxy_cache_bypass $http_upgrade;
        proxy_max_temp_file_size 0;
    }

client ip forward to WAS

nginx
        proxy_set_header    X-Real-Ip        $remote_addr;
        proxy_set_header    X-Fowarded-For   $remote_addr;

gzip

nginx
http {
...
    keepalive_timeout  65;

    gzip  on;
    gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
...
}

max file upload

nginx
client_max_body_size 200M;

nginx for ec2-user

  • /etc/nginx/nginx.conf
nginx
user ec2-user;
sh
chown -R ec2-user:ec2-user /usr/share/nginx/html

http to https

nginx
server {
    # ...
    if ($http_x_forwarded_proto = 'http') {
        return 307 https://$host$request_uri;
    }

http to https without www.

nginx
server {
    server_name  www.okdevtest.net;
    rewrite ^(.*) http://okdevtest.net$1 permanent;
}

server {
    listen 443 ssl http2;
    server_name okdevtest.net;

관련

참고