个人用云计算学习笔记 --20 (Nginx 服务器)
文章目录
- Nginx 服务器
- 1. Nginx 简介
- 2. 安装与基础配置
- 安装步骤
- 基础配置
- hosts 文件配置
- 3. 虚拟主机配置
- 基于名称的虚拟主机
- 基于端口的虚拟主机
- 4. SSL/TLS 配置
- 证书生成
- 站点配置
- 防火墙配置
- 5. 基本认证配置
- 安装工具
- 配置认证
- 创建用户
- 重启服务
- 6. 支持动态脚本
- PHP 支持
- FastCGI 支持
- 7. 反向代理配置
- 角色说明
- 代理服务器配置
- SELinux 设置
- 8. Nginx+Tomcat 动静分离
- 概念
- 配置
- 优势
Nginx 服务器
1. Nginx 简介
- 高性能的 HTTP 和反向代理服务器
- 采用高效的 epoll、kqueue、eventport 等网络 I/O 模型
- 支持高达 5 万个并发连接,资源消耗低,运行稳定
2. 安装与基础配置
安装步骤
# 安装nginx
yum -y install nginx# 启动并设置开机自启
systemctl enable nginx --now
基础配置
# 准备主页
mv /usr/share/nginx/html/index.html{,.ori}
echo Hello World From Nginx > /usr/share/nginx/html/index.html# 防火墙配置
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
hosts 文件配置
- Windows:
C:\Windows\System32\drivers\etc\hosts
- Linux/Unix:
/etc/hosts
- 添加记录格式:
IP地址 域名
(如:10.1.8.10 www.laoma.cloud
)
3. 虚拟主机配置
基于名称的虚拟主机
# 配置文件位置
vim /etc/nginx/conf.d/vhost-name.conf
配置内容:
server {server_name web1.laoma.cloud;root /usr/share/nginx/web1;
}
server {server_name web2.laoma.cloud;root /usr/share/nginx/web2;
}
基于端口的虚拟主机
# 配置文件位置
vim /etc/nginx/conf.d/vhost-port.conf
配置内容:
server {listen 8081;server_name www.laoma.cloud;root /usr/share/nginx/8081;
}
server {listen 8082;server_name www.laoma.cloud;root /usr/share/nginx/8082;
}
4. SSL/TLS 配置
证书生成
# 生成私钥
openssl genrsa -out www.key 2048 # 生成请求文件csr
openssl req -new -key www.key -out www.csr -subj "/C=CN/ST=JS/L=NJ/O=LM/OU=DEVOPS/CN=www.laoma.cloud/emailAddress=laoma@laoma.cloud" # 生成证书
openssl x509 -req -days 3650 -in www.csr -signkey www.key -out www.crt
站点配置
# 证书存放
mkdir /etc/ssl/certs/www.laoma.cloud
mv www* /etc/ssl/certs/www.laoma.cloud# 配置文件
vim /etc/nginx/conf.d/vhost-www.laoma.cloud-ssl.conf
配置内容:
# HTTPS配置
server {listen 443 ssl http2;listen [::]:443 ssl http2;server_name www.laoma.cloud;root /usr/share/nginx/html;ssl_certificate "/etc/ssl/certs/www.laoma.cloud/www.crt";ssl_certificate_key "/etc/ssl/certs/www.laoma.cloud/www.key";
}# HTTP重定向到HTTPS
server {listen 80;listen [::]:80;server_name www.laoma.cloud;return 301 https://$host$request_uri;
}
防火墙配置
firewall-cmd --add-service=https --permanent
firewall-cmd --reload
5. 基本认证配置
安装工具
yum -y install httpd-tools
配置认证
# 编辑配置文件
vim /etc/nginx/conf.d/ssl.conf
添加内容:
server {.....location /auth-basic/ {auth_basic "Basic Auth";auth_basic_user_file "/etc/nginx/.htpasswd";}
}
创建用户
htpasswd -b -c /etc/nginx/.htpasswd 用户名 密码
重启服务
systemctl restart nginx
6. 支持动态脚本
PHP 支持
- 安装 PHP 及相关组件
yum install -y php php-fpm
yum install -y php-gd php-common php-pear php-mbstring php-mcrypt
- 配置 Nginx 解析 PHP
vim /etc/nginx/nginx.conf
添加内容:
server {.....location ~ \.php$ {try_files $uri =404;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}
}
FastCGI 支持
- 安装 fcgiwrap
yum install -y fcgiwrap
- 配置 FastCGI
vim /etc/nginx/fcgiwrap.conf
配置内容:
location /cgi-bin/ {gzip off;root /usr/share/nginx;fastcgi_pass unix:/var/run/fcgiwrap.socket;include /etc/nginx/fastcgi_params;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
- 配置 Systemd 服务
- 创建
/usr/lib/systemd/system/fcgiwrap.service
和/usr/lib/systemd/system/fcgiwrap.socket
文件 - 启动服务:
systemctl enable --now fcgiwrap
7. 反向代理配置
角色说明
- 代理服务器: proxy (10.1.8.20)
- 真实服务器: server (10.1.8.10)
代理服务器配置
# 配置解析
echo '10.1.8.10 www.laoma.cloud' >> /etc/hosts# 安装并启动nginx (同上)# 配置代理
vim /etc/nginx/nginx.conf
配置内容:
server {listen 80 default_server;listen [::]:80 default_server;server_name _;root /usr/share/nginx/html;# 代理设置proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Host $http_host;# 代理规则location /proxy/ {proxy_pass http://www.laoma.cloud/;}
}
SELinux 设置
setsebool -P httpd_can_network_connect on
8. Nginx+Tomcat 动静分离
概念
- 静态资源: html、jpg、css、js 等 (内容固定)
- 动态资源: JSP 等 (内容可能变化)
配置
http {......upstream tomcat {server www.laoma.cloud:8080;}......server {......# 动态资源代理到Tomcatlocation /tomcat/ {proxy_pass http://tomcat/;}}
}
优势
- Nginx 处理静态资源效率高于 Tomcat
- 减轻服务器压力,提高响应速度
- 便于实现 CDN 内容分发