nginx

Nginx คือ ซอฟแวร์สำหรับ web service ที่ใช้จัดการ proxy การทำโหลดบาลานซ์ และ การติดตั้ง SSL certificate โครงสร้างของ Nginx เมื่อติดตั้งเสร็จบน Server Ubuntu โดยสิ่งที่เราจะสนใจจะมีประมาณนี้ครับ

$ cd /etc/nginx
$ ls
.
├── conf.d
├── nginx.conf
├── sites-available
│   └── default
├── sites-enabled
│   └── default

โดยส่วนที่จะ config จริงๆ จะอยู่ที่ไฟล์ default ในโฟลเดอร์ sites-available และ sites-enabled ตัวอย่างดังนี้

# Default server configuration
server {
	listen 80 default_server;
	listen [::]:80 default_server;

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

สามารถ config domain ดังนี้

server_name domain.com;

ตัวอย่างการ config nodejs ที่รัน Server ที่ port 3000 โดยจะ config ภายใน location ดังนี้

location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
}

เมื่อแก้ไข config จะใช้คำสั่งเพื่อตรวจสอบว่า Syntax ถูกต้องหรือไม่? ก่อนที่จะทำการ Reload Nginx

$ sudo systemctl config nginx

#Or

$ sudo nginx -t

คำสั่งที่ใช้ตรวจสอบสถานะของ nginx ดังนี้

$ sudo systemctl status nginx

เมื่อการให้ config ส่วนที่แก้ไขทำงานได้โดยใช้คำสั่ง Restart หรือ Reload ดังนี้

$ sudo systemctl restart nginx

#Or

$ sudo nginx -s reload

#Or

$ sudo systemctl reload nginx

Config SSL Certificate ด้วย Certbot

สามารถ config certificate โดยใช้ certbot ดังนี้

$ sudo certbot --nginx

config จะถูกสร้างขั้นที่ไฟล์ default

$ cd /etc/nginx/sites-available
$ ls
$ cat default

config ssl certificate ที่ถูกสร้างขี้นจะอยู่ในส่วนนี้

listen [::]:443 ssl; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

สามารถ config สร้างเฉพาะ certificate โดยใช้ certbot ดังนี้

$ sudo certbot certonly --nginx

ไฟล์ certificate และ private key จะถูกสร้างขึ้นที่ /etc/letsencrypt/live

$ cd /etc/letsencrypt/live

ตัวอย่าง nginx reverse proxy multiple Node.js apps โดย subfolders

เมื่อมี Service Node ที่รันด้วย port 3001 สามารถเพิ่ม config ได้ดังนี้

location /app2/ {
    rewrite ^/app2/(.*)$ $1 break;

    proxy_http_version 1.1;
    proxy_cache_bypass $http_upgrade;

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    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_pass http://localhost:3001;
}

เพียงเท่านี้เมื่อเรียกมายัง path ภายใน /app2 ก็จะถูกเรียกมายัง port 3001 ของ service ที่รันอยู่