Nginx - low-hill/Knowledge GitHub Wiki

Nginx port

  • Nginx๋Š” default port ๋Š” 80์ด๊ณ , SSL secure connection์€ 443port์—์„œ ๋ฐœ์ƒํ•œ๋‹ค.

  • Uncomplicated firewall์ •๋ณด๋กœ ์„œ๋ฒ„์˜ open port ์ •๋ณด ํ™•์ธํ•˜๊ธฐ

    • sudo ufw status
  • ์„ค์ •๊ฐ€๋Šฅํ•œ application list ํ™•์ธํ•˜๊ธฐ

    • sudo ufw app list
    • output์— ๋Œ€ํ•œ ํ•ด์„
Available applications:

  Nginx Full       // 80(unencrypted traffic) ๋ฐ 443(TLS/SSL) port ์˜คํ”ˆ
  Nginx HTTP       // 80 port๋งŒ ์˜คํ”ˆ 
  Nginx HTTPS.     // 443 port๋งŒ ์˜คํ”ˆ
  • 80 port ์˜คํ”ˆ ํ›„ ์ƒํƒœ ํ™•์ธ
    • sudo ufw allow 'Nginx HTTP'

    • sudo ufw status


Nginx configuration ์„ค์ •

sudo vim /etc/nginx/sites-available/example.com

  • configuration ํŒŒ์ผ ๋‚ด์šฉ์€ ํ•˜์œ„์™€ ๊ฐ™๋‹ค.
server {
     listen 80; 
     listen [::]:80;       // IPv6 addresses

     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;
      }
}
  • configuration ํŒŒ์ผ syntaxํ™•์ธ ๋ช…๋ น์–ด๋Š” ํ•˜์œ„์™€ ๊ฐ™๋‹ค.
    • sudo nginx -t
  • configuration ํŒŒ์ผ ์ˆ˜์ • ์‚ฌํ•ญ ์ ์šฉ์„ ์œ„ํ•œ ํ•˜์œ„ ๋ช…๋ น์–ด๋กœ ์žฌ๊ธฐ๋™ ํ•œ๋‹ค. * sudo systemctl restart nginx

Nginx active ์ƒํƒœ ํ™•์ธํ•˜๊ธฐ

  • systemctl status nginx

    • active์ƒํƒœ์˜ output์€ ํ•˜์œ„์™€ ๊ฐ™๋‹ค.

    • Active: active (running) since ์‹œ๊ฐ„ ์ •๋ณด

    • inactive ์ƒํƒœ ์‹œ output

    • inactive (dead)...

Nginx ๊ธฐ๋™ command

sudo systemctl start nginx

sudo systemctl reload nginx

Nginx error ํ™•์ธํ•˜๊ธฐ

  • sudo cat /var/log/nginx/error.log

Maximum Throughput์„ ์œ„ํ•œ Nginx ํŠœ๋‹

  • worker_processes

    • default 1, ๋‹จ์ผ ํ”„๋กœ์„ธ์Šค maximun์€ 1024
    • CPU Core ์ˆ˜ ํ˜น์€ CPU Core * 2๋กœ ์„ค์ •
      • CPU Core์ˆ˜ ํ™•์ธ ๋ฐฉ๋ฒ• -> top command ์‹คํ–‰ ํ›„ ์ˆซ์ž 1
    • ๋ณ€๊ฒฝ๋œ worker process ๊ฐฏ์ˆ˜ ํ™•์ธ ๋ช…๋ น์–ด
      • ps -aux | grep nginx |grep -v grep
  • worker_rlimit_nofile ์ˆ˜์ •

    • ulimit -n ์ˆ˜์™€ ๋™์ผํ•˜๊ฒŒ ์„ค์ •ํ•˜๋Š”๊ฒƒ์ด ์ข‹์Œ
  • worker_connections ์ˆ˜์ •

    worker_connections: worker process๋‹น ๋™์‹œ์ ‘์†์ž์ˆ˜๋กœ default๊ฐ’์€ 1024, ์ตœ๋Œ€๋Š” 65535, os์˜ file descriptors maximum๊ฐ’ ๋ฒ”์œ„ ์•ˆ์—์„œ ์„ค์ • ํ•˜์œ„ ์ฒ˜๋Ÿผ worker_connections์— ๋”ฐ๋ผ ์ตœ๋Œ€ ์ ‘์†์ž ์ˆ˜๊ฐ€ ๊ฒฐ์ •๋œ๋‹ค. max_clients = worker_processes * worker_connections

    In a reverse proxy situation max_clients = worker_processes * worker_connections/4


events {
    #  event-models์€ epoll์„ ์‚ฌ์šฉ(ํšจ์œจ๋†’์ž„)
    use epoll;
    # ์„œ๋ฒ„์˜ ์„ฑ๋Šฅ ๋ฐ ๋ฉ”๋ชจ๋ฆฌ์— ๋”ฐ๋ผ ์„ค์ •
    worker_connections 10240;
    #์ƒˆ์—ฐ๊ฒฐ์— ๋Œ€ํ•œ ์•Œ๋ฆผ์„ ๋ฐ›์€ ํ›„ ๋งŽ์€ ์—ฐ๊ฒฐ์„ ํ—ˆ์šฉ, default๋Š” off
    multi_accept on;
}
  • timeout ๋“ฑ ์„ค์ •์œผ๋กœ ์„œ๋ฒ„ ๋ฆฌ์†Œ์Šค, CPU, ๋ฉ”๋ชจ๋ฆฌ, ์—ฐ๊ฒฐ ์ œ์–ด.
keepalive_timeout 60;              
tcp_nodelay on;
client_header_buffer_size 4k;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
client_header_timeout 15;
client_body_timeout 15;
reset_timeout_connection on;
send_timeout 15;
server_tokens off;
client_max_body_size 10m;
  • GZIP์••์ถ• ์„ค์ •
gzip on;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain text/css text/javascript application/json application/javascript;
gzip_vary on;

Reference

Common Nginx Connection Errors

Nginx ํŠœ๋‹

nginx config