RHEL8中怎么部署Nginx Web服务

发布时间:2022-02-16 15:41:22 作者:iii
来源:亿速云 阅读:225
# 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 服务管理命令

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服务的安全稳定运行。 “`

推荐阅读:
  1. 部署LNMP架构之安装nginx服务
  2. nginx+docker+nfs部署

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

rhel nginx web

上一篇:javascript函数怎么用var定义

下一篇:nginx首页中文乱码怎么解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》