Cài đặt NginX trên CentOS/RHEL
Thực hiện lệnh sau để tạo ra file /etc/yum.repos.d/nginx.repo
echo '[nginx] name=nginx repo baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/ gpgcheck=0 enabled=1' > /etc/yum.repos.d/nginx.repo
Thực hiện lệnh cài đặt
yum install nginx # kích hoạt dịch vụ systemctl enable nginx # Chạy dịch vụ systemctl start nginx
Cấu hình Nginx
File cấu hình
Để kiểm tra cấu hình thực hiện lệnh
nginx -t
Nó cho biết cấu hình có lỗi không, cũng cho biết đường dẫn file cấu hình là tại /etc/nginx/nginx.conf
Ngoài ra trong nginx.conf thường cũng có chỉ thị trong khối http là
include /etc/nginx/conf.d/*.conf;
Nên khi nạp cấu hình trong file /etc/nginx/nginx.conf nó cũng nạp các file
.conf trong thư mục /etc/nginx/conf.d/
Vậy để viết cấu hình tạo virtual host - cho nginx bạn có thể viết trong /etc/nginx/nginx.conf hoặc file .conf
bất kỳ bạn tạo ra trong /etc/nginx/conf.d/
File nginx.conf cơ bản
user nginx;
# Nginx gồm một tiến trình trình (process) chính và các tiến trình con (worker_processes)
# Tiến trình chính để nạp cấu hình và quản lý các tiến trình con, các tiến trình con
# thì nhận các yêu cầu truy vấn gửi đến và xử lý nó. Mỗi tiến trình con chạy trên một nhân CPU
# nên tốt nhất thiết lập số tiến trình con bằng số nhân CPU bạn có, lấy bằng lệnh
# cat /proc/cpuinfo | grep processor | wc -l
worker_processes 8;
# error_log /var/log/nginx/error.log crit;
error_log off;
pid /run/nginx.pid;
# worker_rlimit_nofile = worker_connections*2
worker_rlimit_nofile 10000;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
# Thiết lập các worker xử lý thế nào khi có các sự kiện kết nối
events {
# Số client phục vụ đồng thời bởi nginx.
# Kết hợp với Worker processes, số lớn nhất trên mỗi giây tính theo
# Worker processes * Worker connections
# Thiết lập bởi số process đồng thời cho phép mỗi core, dùng lệnh ulimit -n để biết
worker_connections 1024;
use epoll;
# On chấp nhận các kết nối mới một lúc, Off từng kết nối một được chấp nhận
# On chấp nhận kết nối mới nhiều nhất có thể, cận thận bị ngập kết nối
multi_accept off;
}
http {
# 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;
# Tắt lưu log giảm tải truy cập vào đĩa
access_log off;
# https://nginx.org/en/docs/http/ngx_http_core_module.html#open_file_cache
open_file_cache max=200000 inactive=5m;
open_file_cache_valid 60s;
open_file_cache_min_uses 5;
open_file_cache_errors on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
types_hash_max_size 2048;
# allow the server to close connection on non responding client, this will free up memory
reset_timedout_connection on;
# Off header version nginx
server_tokens off;
gzip on;
gzip_min_length 10240;
gzip_comp_level 1;
gzip_vary on;
gzip_disable msie6;
gzip_proxied expired no-cache no-store private auth;
gzip_types
# text/html is always compressed by HttpGzipModule
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/x-javascript
application/json
application/xml
application/rss+xml
application/atom+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
# các khối server
server {
# ...
}
server {
# ...
}
}
Trong file cấu hình trên, để tạo ra các virtual host chỉ việc tạo ra các khối server (trong khối http) thích hợp.
Ví dụ, Nginx với PHP-FPM
server {
listen 80;
root /home/pathtowebsite/public/;
index index.php;
server_name myweb.net www.myweb.net;
location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
# hoặc dùng sock
# fastcgi_pass unix:/usr/local/php/var/run/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param REMOTE_ADDR $http_x_real_ip;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|webp|tiff|ttf|svg)$ {
expires 5d;
}
# deny access to . files, for security
location ~ /\. {
log_not_found off;
deny all;
}
}
Nginx với PHP-FPM, cổng 443 (SSL)
server {
listen 80;
root /home/pathtowebsite/public/;
index index.php;
server_name myweb.net www.myweb.net;
# chuyển hướng www về non-www
if ($host = www.myweb.net) {
return 301 https://myweb.net$request_uri;
}
# Cấu hình xác thực SSL
# xem https://xuanthulab.net/ssl-tls-va-giao-thuc-https-voi-open-ssl-va-let-s-encrypt.html
ssl_certificate /đường đến/file/chứng chỉ
ssl_certificate_key /đường đến/file/key
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
# hoặc dùng sock
# fastcgi_pass unix:/usr/local/php/var/run/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param REMOTE_ADDR $http_x_real_ip;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|webp|tiff|ttf|svg)$ {
expires 5d;
}
# deny access to . files, for security
location ~ /\. {
log_not_found off;
deny all;
}
}
Chuyển hướng http(80) sang https(443)
server {
if ($host = mysite.net) {
return 301 https://$host$request_uri;
}
listen 80;
server_name mysite.net www.mysite.net;
return 301 https://mysite.net$request_uri;
}
Sử dụng như một proxy (cổng 80)
Ví dụ, ứng dụng .NET đang ở cổng 5000 có thể cấu hình nginx như sau
server {
listen 80;
server_name mysite.net *.mysite.net;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Sử dụng như một proxy (cổng 443)
Ví dụ, ứng dụng .NET đang ở cổng 5000 (localhost:5000) có thể cấu hình nginx như sau
include /etc/nginx/proxy.conf;
limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;
server_tokens off;
client_body_timeout 10; client_header_timeout 10; send_timeout 10;
# tạo upstream - nơi nginx chuyển http message đến
upstream mvcblog {
server localhost:5000;
}
# chuyển hướng 80 -> 443
server {
listen 80;
server_name mysite.net *.mysite.net;
add_header Strict-Transport-Security max-age=15768000;
return 301 https://$host$request_uri;
}
server {
listen *:443 ssl;
server_name mysite.net;
ssl_certificate /đường đến/file/chứng chỉ
ssl_certificate_key /đường đến/file/key
ssl_protocols TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_ecdh_curve secp384r1;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on; #ensure your cert is capable
ssl_stapling_verify on; #ensure your cert is capable
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
#Redirects all traffic
location / {
proxy_pass http://mvcblog;
limit_req zone=one burst=10 nodelay;
}
}
Trong đó file /etc/nginx/proxy.conf có nội dung
proxy_redirect off; 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; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffers 32 4k;
