溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

RHEL8中怎么部署Nginx Web服務

發布時間:2022-02-16 15:41:22 來源:億速云 閱讀:227 作者:iii 欄目:開發技術
# 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  # 如有內核更新建議重啟

1.3 防火墻配置

RHEL8默認啟用firewalld防火墻,需要開放HTTP(80)和HTTPS(443)端口:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

1.4 SELinux配置

RHEL8默認啟用SELinux,需要為Nginx設置正確的安全上下文:

sudo setsebool -P httpd_can_network_connect 1

二、Nginx安裝

2.1 從官方倉庫安裝

RHEL8默認倉庫包含Nginx,可直接安裝:

sudo dnf install nginx -y

2.2 從Nginx官方倉庫安裝(推薦)

如需最新穩定版,可添加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

2.3 驗證安裝

安裝完成后驗證版本:

nginx -v

輸出應類似:

nginx version: nginx/1.20.1

三、Nginx基本配置

3.1 服務管理命令

  • 啟動Nginx:

    sudo systemctl start nginx
    
  • 設置開機自啟:

    sudo systemctl enable nginx
    
  • 檢查狀態:

    sudo systemctl status nginx
    

3.2 配置文件結構

Nginx主要配置文件位于: - /etc/nginx/nginx.conf:主配置文件 - /etc/nginx/conf.d/:附加配置文件目錄 - /etc/nginx/sites-available/:虛擬主機配置(需手動創建) - /etc/nginx/sites-enabled/:啟用的虛擬主機(需手動創建)

3.3 基本配置示例

編輯主配置文件:

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

3.4 測試配置

每次修改配置后都應測試:

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

四、虛擬主機配置

4.1 創建網站目錄

sudo mkdir -p /var/www/example.com/html
sudo chown -R nginx:nginx /var/www/example.com
sudo chmod -R 755 /var/www

4.2 創建虛擬主機配置文件

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

4.3 創建測試頁面

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>

五、SSL/TLS配置

5.1 安裝Certbot

sudo dnf install certbot python3-certbot-nginx -y

5.2 獲取證書

sudo certbot --nginx -d example.com -d www.example.com

5.3 自動續期測試

sudo certbot renew --dry-run

5.4 配置自動續期

編輯crontab:

sudo crontab -e

添加:

0 0,12 * * * /usr/bin/certbot renew --quiet

六、安全加固

6.1 隱藏Nginx版本信息

在http塊中添加:

server_tokens off;

6.2 防止點擊劫持

在server塊中添加:

add_header X-Frame-Options "SAMEORIGIN";

6.3 XSS防護

add_header X-XSS-Protection "1; mode=block";

6.4 內容安全策略

add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;

6.5 禁用不需要的HTTP方法

if ($request_method !~ ^(GET|HEAD|POST)$ ) {
    return 405;
}

七、性能優化

7.1 調整worker進程

worker_processes auto;  # 自動設置為CPU核心數
worker_rlimit_nofile 65535;  # 每個worker能打開的文件描述符數量

7.2 事件模塊優化

events {
    worker_connections 4096;
    use epoll;
    multi_accept on;
}

7.3 啟用Gzip壓縮

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;

7.4 緩存靜態資源

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 365d;
    add_header Cache-Control "public, no-transform";
}

八、日志管理

8.1 日志輪轉

創建日志輪轉配置:

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
}

8.2 日志分析

安裝GoAccess進行實時日志分析:

sudo dnf install goaccess -y

使用:

goaccess /var/log/nginx/access.log -o /var/www/html/report.html --log-format=COMBINED

九、常見問題排查

9.1 502 Bad Gateway

可能原因: - 后端服務未運行 - 權限問題 - 資源不足

檢查:

sudo tail -f /var/log/nginx/error.log

9.2 403 Forbidden

可能原因: - 文件權限不正確 - SELinux限制 - 目錄索引被禁用

解決方案:

sudo chown -R nginx:nginx /var/www
sudo chmod -R 755 /var/www
sudo restorecon -Rv /var/www

9.3 性能問題排查

使用工具:

top
htop
nginx -T  # 查看完整配置
ss -tulnp | grep nginx

十、進階配置

10.1 負載均衡配置

upstream backend {
    least_conn;
    server backend1.example.com weight=5;
    server backend2.example.com;
    server backend3.example.com;
}

server {
    location / {
        proxy_pass http://backend;
    }
}

10.2 HTTP/2啟用

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    # SSL配置...
}

10.3 反向代理配置

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服務的安全穩定運行。 “`

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女