# RHEL8中怎么部署Nginx Web服務
## 前言
在當今互聯網時代,Web服務已成為企業信息化建設的基礎設施。Nginx作為一款高性能的開源Web服務器,以其輕量級、高并發處理能力和豐富的功能模塊,在全球Web服務器市場中占據重要地位。本文將詳細介紹在Red Hat Enterprise Linux 8(RHEL8)系統中部署Nginx Web服務的完整流程,包括環境準備、安裝配置、安全加固以及性能優化等內容。
## 一、環境準備
### 1.1 系統要求
在開始部署前,請確保您的RHEL8系統滿足以下要求:
- 最小化安裝的RHEL8系統(推薦)
- 至少1GB可用內存
- 10GB以上磁盤空間
- 配置正確的網絡連接
- root或具有sudo權限的普通用戶
### 1.2 系統更新
首先更新系統軟件包到最新版本:
```bash
sudo dnf update -y
sudo reboot # 如有內核更新建議重啟
RHEL8默認啟用firewalld防火墻,需要開放HTTP(80)和HTTPS(443)端口:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
RHEL8默認啟用SELinux,需要為Nginx設置正確的安全上下文:
sudo setsebool -P httpd_can_network_connect 1
RHEL8默認倉庫包含Nginx,可直接安裝:
sudo dnf install nginx -y
如需最新穩定版,可添加Nginx官方倉庫:
sudo tee /etc/yum.repos.d/nginx.repo <<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
sudo dnf install nginx -y
安裝完成后驗證版本:
nginx -v
輸出應類似:
nginx version: nginx/1.20.1
啟動Nginx:
sudo systemctl start nginx
設置開機自啟:
sudo systemctl enable nginx
檢查狀態:
sudo systemctl status nginx
Nginx主要配置文件位于:
- /etc/nginx/nginx.conf
:主配置文件
- /etc/nginx/conf.d/
:附加配置文件目錄
- /etc/nginx/sites-available/
:虛擬主機配置(需手動創建)
- /etc/nginx/sites-enabled/
:啟用的虛擬主機(需手動創建)
編輯主配置文件:
sudo vi /etc/nginx/nginx.conf
典型配置示例:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
每次修改配置后都應測試:
sudo nginx -t
正確輸出:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
然后重載配置:
sudo systemctl reload nginx
sudo mkdir -p /var/www/example.com/html
sudo chown -R nginx:nginx /var/www/example.com
sudo chmod -R 755 /var/www
sudo vi /etc/nginx/conf.d/example.com.conf
示例配置:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
location ~ /\.ht {
deny all;
}
}
sudo vi /var/www/example.com/html/index.html
內容示例:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Example.com</title>
</head>
<body>
<h1>Success! The example.com server is working!</h1>
</body>
</html>
sudo dnf install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run
編輯crontab:
sudo crontab -e
添加:
0 0,12 * * * /usr/bin/certbot renew --quiet
在http塊中添加:
server_tokens off;
在server塊中添加:
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
worker_processes auto; # 自動設置為CPU核心數
worker_rlimit_nofile 65535; # 每個worker能打開的文件描述符數量
events {
worker_connections 4096;
use epoll;
multi_accept on;
}
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
add_header Cache-Control "public, no-transform";
}
創建日志輪轉配置:
sudo vi /etc/logrotate.d/nginx
內容:
/var/log/nginx/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
安裝GoAccess進行實時日志分析:
sudo dnf install goaccess -y
使用:
goaccess /var/log/nginx/access.log -o /var/www/html/report.html --log-format=COMBINED
可能原因: - 后端服務未運行 - 權限問題 - 資源不足
檢查:
sudo tail -f /var/log/nginx/error.log
可能原因: - 文件權限不正確 - SELinux限制 - 目錄索引被禁用
解決方案:
sudo chown -R nginx:nginx /var/www
sudo chmod -R 755 /var/www
sudo restorecon -Rv /var/www
使用工具:
top
htop
nginx -T # 查看完整配置
ss -tulnp | grep nginx
upstream backend {
least_conn;
server backend1.example.com weight=5;
server backend2.example.com;
server backend3.example.com;
}
server {
location / {
proxy_pass http://backend;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
# SSL配置...
}
location /app/ {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
通過本文的詳細指導,您應該已經在RHEL8系統上成功部署了Nginx Web服務,并進行了基本的安全加固和性能優化。Nginx的強大功能遠不止于此,您還可以根據實際需求進一步探索其豐富的模塊和配置選項,如WebSocket支持、流媒體服務、微緩存等高級功能。
建議定期檢查Nginx官方文檔和安全性公告,保持服務更新,確保Web服務的安全穩定運行。 “`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。