1. 首页 > Linux教程 > 正文

Linux教程FG232-HTTP服务器配置(Nginx)

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

本文档详细介绍Nginx HTTP服务器的安装、配置和管理方法。

from PG视频:www.itpux.com

Part01-Nginx安装

1.1 安装Nginx服务

# 安装Nginx
$ sudo dnf install -y nginx
Last metadata expiration check: 0:45:23 ago on Thu 03 Apr 2026 22:15:15 AM CST.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
nginx x86_64 1:1.20.1-14.el9 appstream 36 k
Installing dependencies:
nginx-filesystem
noarch 1:1.20.1-14.el9 appstream 11 k

Transaction Summary
================================================================================
Install 2 Packages

Total download size: 47 k
Installed size: 100 k
Downloading Packages:
(1/2): nginx-1.20.1-14.el9.x86_64.rpm 36 kB/s | 36 kB 00:01
(2/2): nginx-filesystem-1.20.1-14.el9.noarch.rpm 11 kB/s | 11 kB 00:01
——————————————————————————–
Total 47 kB/s | 47 kB 00:01
Running transaction check
Transaction check succeeded.更多视频教程www.fgedu.net.cn
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : nginx-filesystem-1:1.20.1-14.el9.noarch 1/2
Installing : nginx-1:1.20.1-14.el9.x86_64 2/2
Running scriptlet: nginx-1:1.20.1-14.el9.x86_64 2/2
Verifying : nginx-filesystem-1:1.20.1-14.el9.noarch 1/2
Verifying : nginx-1:1.20.1-14.el9.x86_64 2/2

Installed:
nginx-1:1.20.1-14.el9.x86_64
nginx-filesystem-1:1.20.1-14.el9.noarch

Complete!

# 启动Nginx服务
$ sudo systemctl start nginx

# 设置开机自启动
$ sudo systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

# 查看服务状态
$ sudo systemctl status nginx
● nginx.service – The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
Active: active (running) since Thu 2026-04-03 22:15:00 CST; 10s ago
Process: 12350 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 12351 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 12352 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 12353 (nginx)
Tasks: 2 (limit: 49152)
Memory: 2.5M
CPU: 20ms
CGroup: /system.slice/nginx.service
├─12353 “nginx: master process /usr/sbin/nginx”
└─12354 “nginx: worker process”

Apr 03 22:15:00 rhel10 systemd[1]: Starting The nginx HTTP and reverse proxy server…
Apr 03 22:15:00 rhel10 nginx[12351]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Apr 03 22:15:00 rhel10 nginx[12351]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Apr 03 22:15:00 rhel10 systemd[1]: Started The nginx HTTP and reverse proxy server.

# 配置防火墙
$ sudo firewall-cmd –permanent –add-service=http
success
$ sudo firewall-cmd –permanent –add-service=https
success
$ sudo firewall-cmd –reload
success

# 测试访问
$ curl http://localhost

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and
working. Further conf

风哥提示:

iguration is required.

Part02-Nginx配置文件

2.1 主配置文件

# 查看主配置文件
$ cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
worker_connections 1024;
}

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;

sendfile on;
tcp_nopush on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

include /etc/nginx/conf.d/*.conf;
}

# 查看配置文件目录结构
$ tree /etc/nginx/
/etc/nginx/
├── conf.d
│ └── default.conf
├── default.d
├── fastcgi_params
├── mime.types
├── nginx.conf
├── scgi_params
└── uwsgi_params

# 创建测试页面
$ echo “

Hello Nginx

” | sudo tee /usr/share/nginx/html/index.html

Hello Nginx

# 测试访问
$ curl http://localhost

Hello Nginx

Part03-虚拟主机配置

3.1 配置Server块

# 创建网站目录
$ sudo mkdir -p /var/www/fgedu.net.cn/html
$ sudo mkdir -p /var/www/fgedu.net.cn/logs

# 创建测试页面
$ echo “

Welcome to fgedu.net.cn

” | sudo tee /var/www/fgedu.net.cn/html/index.html

# 创建虚拟主机配置
$ sudo tee /etc/nginx/conf.d/fgedu.net.cn.conf << EOF server { listen 80; server_name fgedu.net.cn www.fgedu.net.cn; root /var/www/fgedu.net.cn/html; index index.html index.htm; access_log /var/www/fgedu.net.cn/logs/access.log; error_log /var/www/fgedu.net.cn/logs/error.log; location / { try_files \$uri \$uri/ =404; } location ~ \.php$ { fastcgi_pass unix:/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; include fastcgi_params; } } EOF # 检查配置语法 $ 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 # 测试访问 $ curl -H "Host: fgedu.net.cn" http://localhost

Welcome to fgedu.net.cn

Part04-反向代理配置

4.1 配置反向代理

# 配置反向代理
$ sudo tee /etc/nginx/conf.d/proxy.conf << EOF upstream backend { server 192.168.1.101:8080; server 192.168.1.102:8080; server 192.168.1.103:8080; } server { listen 80; server_name proxy.fgedu.net.cn; location / { proxy_pass http://backend; 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; } } EOF # 配置负载均衡 $ sudo tee /etc/nginx/conf.d/loadbalance.conf << EOF upstream backend { least_conn; server 192.168.1.101:8080 weight=3; server 192.168.1.102:8080 weight=2; server 192.168.1.103:8080 weight=1 backup; keepalive 32; } server { listen 80; server_name lb.fgedu.net.cn; location / { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; } } EOF # 检查配置 $ 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

Part05-Nginx性能优化

更多学习教程公众号风哥教程itpux_com

5.1 性能优化配置

# 优化主配置文件
$ sudo tee /etc/nginx/nginx.conf << EOF user nginx; worker_processes auto; worker_rlimit_nofile 65535; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 4096; 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; gzip学习交流加群风哥QQ113257174 on; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss; open_file_cache max=10000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; client_body_buffer_size 128k; client_max_body_size 10m; include /et学习交流加群风哥微信: itpux-comc/nginx/conf.d/*.conf; } EOF # 配置SSL $ sudo tee /etc/nginx/conf.d/ssl.conf << EOF server { listen 443 ssl http2; server_name fgedu.net.cn; ssl_certificate /etc/pki/tls/certs/fgedu.net.cn.crt; ssl_certificate_key /etc/pki/tls/private/fgedu.net.cn.key; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; root /var/www/fgedu.net.cn/html; index index.html; location / { try_files \$uri \$uri/ =404; } } server { listen 80; server_name fgedu.net.cn; return 301 https://\$server_name\$request_uri; } EOF # 检查配置 $ 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 # 查看Nginx状态 $ sudo systemctl status nginx ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled) Active: active (running) since Thu 2026-04-03 22:20:00 CST; 10s ago
风哥针对配置建议:
1. 使用Nginx作为反向代理和负载均衡器
2. 启用gzip压缩提高传输效率
3. 配置SSL证书启用HTTPS
4. 优化worker进程和连接数
5. 配置缓存提高性能

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息