您好,登录后才能下订单哦!
# 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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。