Nginx 服务器
文章目录
Nginx是一款高性能的HTTP和反向代理服务器,能够选择高效的epoll、kqueue、eventport最为网络I/O模型,在高连接并发的情况下,能够支持高达5万个并发连接数的响应,而内存、CPU等系统资源消耗却非常低,运行非常稳定。
安装 nginx
# 安装 nginx
[root@www ~]# yum -y install nginx
# 启动 nginx
[root@www ~]# systemctl enable nginx --now
# 准备主页
[root@www ~]# mv /usr/share/nginx/html/index.html{,.ori}
[root@www ~]# echo Hello World From Nginx > /usr/share/nginx/html/index.html
# 防火墙
[root@www ~]# firewall-cmd --add-service=http --permanent
[root@www ~]# firewall-cmd --reload
[root@client ~]# curl http://www.shizhan.cloud
# windows客户端修改C:\Windows\System32\drivers\etc\hosts
# Linux或Unix修改 /etc/hosts
# 添加如下记录
10.1.8.10 www.shizhan.cloud
虚拟主机
同一个web服务器提供多个站点。
根据名称
# 参考主配置文件/etc/nginx/nginx.conf中server块配置
[root@www ~]# vim /etc/nginx/conf.d/vhost-name.conf
server {
server_name web1.shizhan.cloud;
root /usr/share/nginx/web1;
}
server {
server_name web2.shizhan.cloud;
root /usr/share/nginx/web2;
}
[root@www ~]# mkdir /usr/share/nginx/web{1,2}
[root@www ~]# echo web1.shizhan.cloud > /usr/share/nginx/web1/index.html
[root@www ~]# echo web2.shizhan.cloud > /usr/share/nginx/web2/index.html
[root@www ~]# systemctl restart nginx
客户端测试
# 配置名称解析,假设web服务器ip地址为10.1.8.10
10.1.8.10 web1.shizhan.cloud web2.shizhan.cloud
[root@client ~]# curl http://www1.shizhan.cloud/
www1.shizhan.cloud
[root@client ~]# curl http://www2.shizhan.cloud/
www2.shizhan.cloud
提示:清理环境,避免影响后续实验。
[root@www ~]# mv /etc/nginx/conf.d/vhost* /etc/nginx/conf.d/bak
根据 port
[root@www ~]# vim /etc/nginx/conf.d/vhost-port.conf
server {
listen 8081;
server_name www.shizhan.cloud;
root /usr/share/nginx/8081;
}
server {
listen 8082;
server_name www.shizhan.cloud;
root /usr/share/nginx/8082;
}
[root@www ~]# mkdir /usr/share/nginx/808{1,2}
[root@www ~]# echo 8081 > /usr/share/nginx/8081/index.html
[root@www ~]# echo 8082 > /usr/share/nginx/8082/index.html
[root@www ~]# systemctl restart nginx
客户端测试
# 配置名称解析,假设web服务器ip地址为10.1.8.10
10.1.8.10 www.shizhan.cloud
[root@client ~]# curl http://www.shizhan.cloud:8081
8081
[root@client ~]# curl http://www.shizhan.cloud:8082
8082
提示:清理环境,避免影响后续实验。
[root@www ~]# mv /etc/nginx/conf.d/vhost* /etc/nginx/conf.d/bak
配置SSL/TLS
生成证书
#--1--生成私钥
[root@www ~]# mkdir certs && cd certs
[root@www certs]# openssl genrsa -out www.key 2048
#--2--生成请求文件csr
[root@www certs]# openssl req -new -key www.key -out www.csr -subj "/C=CN/ST=JS/L=NJ/O=LM/OU=DEVOPS/CN=www.shizhan.cloud/emailAddress=shizhan@shizhan.cloud"
#CN的值必须是网站域名
#--3--使用自己的私钥对请求文件签名,以生成证书
[root@www certs]# openssl x509 -req -days 3650 -in www.csr -signkey www.key -out www.crt
配置站点
[root@www certs]# mkdir /etc/ssl/certs/www.shizhan.cloud
[root@www certs]# mv www* /etc/ssl/certs/www.shizhan.cloud
# 参照默认配置修改
[root@www ~]# cp /etc/nginx/nginx.conf /etc/nginx/conf.d/vhost-www.shizhan.cloud-ssl.conf
[root@www ~]# vim /etc/nginx/conf.d/vhost-www.shizhan.cloud-ssl.conf
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.shizhan.cloud;
root /usr/share/nginx/html;
# 证书
ssl_certificate "/etc/ssl/certs/www.shizhan.cloud/www.crt";
# 私钥
ssl_certificate_key "/etc/ssl/certs/www.shizhan.cloud/www.key";
}
[root@www ~]# systemctl restart nginx
配置HTTP重定向到https
[root@www ~]# vim /etc/nginx/conf.d/vhost-www.shizhan.cloud-ssl.conf
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.shizhan.cloud;
root /usr/share/nginx/html;
# 证书
ssl_certificate "/etc/ssl/certs/www.shizhan.cloud/www.crt";
# 私钥
ssl_certificate_key "/etc/ssl/certs/www.shizhan.cloud/www.key";
}
# 配置HTTP重定向到https
server {
listen 80;
listen [::]:80;
server_name www.shizhan.cloud;
root /usr/share/nginx/html;
# 添加重定向
return 301 https://$host$request_uri;
}
[root@www ~]# systemctl restart nginx
# 防火墙设置
[root@www ~]# firewall-cmd --add-service=https --permanent
[root@www ~]# firewall-cmd --reload
# 测试
[root@client ~]# curl http://www.shizhan.cloud/
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
# 使用-k指明目标站点不是一个安全站点
[root@client ~]# curl -k https://www.shizhan.cloud/
配置基本认证
用户名和密码使用plain text发送,所以最好配置SSL/TLS。
#安装工具
[root@www ~]# yum -y install httpd-tools
[root@www ~]# vim /etc/nginx/conf.d/ssl.conf
# add into the [server] section
server {
.....
location /auth-basic/ {
auth_basic "Basic Auth";
auth_basic_user_file "/etc/nginx/.htpasswd";
}
}
[root@www ~]# systemctl restart nginx
# add user for Basic authentication
[root@www ~]# yum install -y httpd-tools
[root@www ~]# htpasswd -b -c /etc/nginx/.htpasswd shizhan 123456
# create a test page
[root@www ~]# mkdir /usr/share/nginx/html/auth-basic
[root@www ~]# vim /usr/share/nginx/html/auth-basic/index.html
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: shizhaner;">
Test Page for Basic Authentication
</div>
</body>
</html>
# 测试,通过-u选项指定用户名和密码
[root@client ~]# curl -ku shizhan:123456 https://www.shizhan.cloud/auth-basic/
支持动态脚本
使用 PHP
# 安装PHP和php-fpm,建议把其他的扩展包一起安装
[root@www ~]# yum install -y php php-fpm
# php-fpm: 负责接收web程序发来的php代码
# php:负责解析和执行php代码,并将结果返回给php-fpm
# php-fpm 将结果返回给web程序,web程序将结果返回给客户端
# 建议把其他的扩展包一起安装
[root@www ~]# yum install -y php-gd php-common php-pear php-mbstring php-mcrypt
# 查看 php 版本
[root@www ~]# php -v
# 测试 php 是否正常
[root@www ~]# echo "<?php echo 'PHP Test Page'.\"\n\"; ?>" > php_test.php
[root@www ~]# php php_test.php
PHP Test Page
# 准备测试页,使用phpinfo查看详细信息
[root@www ~]# echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php
配置虚拟机主机支持php
# 修改配置文件
[root@www ~]# vim /etc/nginx/conf.d/vhost-www.shizhan.cloud-ssl.conf
# add into the [server] section
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.shizhan.cloud;
root /usr/share/nginx/html;
ssl_certificate "/etc/ssl/certs/www.shizhan.cloud/www.crt";
ssl_certificate_key "/etc/ssl/certs/www.shizhan.cloud/www.key";
# 添加代理
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;
}
}
# 配置HTTP重定向到https
server {
listen 80;
listen [::]:80;
server_name www.shizhan.cloud;
root /usr/share/nginx/html;
# 添加重定向
return 301 https://$host$request_uri;
}
客户端测试
[root@client ~]# curl -k https://www.shizhan.cloud/info.php
使用 FastCGI
# install from EPEL
[root@www ~]# yum install -y fcgiwrap
[root@www ~]# vim /etc/nginx/fcgiwrap.conf
# for example, enable CGI under [/cgi-bin]
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;
}
[root@www ~]# mkdir -m 755 /usr/share/nginx/cgi-bin
[root@www ~]# vim /etc/nginx/conf.d/ssl.conf
# add settings into [server] section of a site definition
server {
.....
include fcgiwrap.conf;
}
[root@www ~]# systemctl restart nginx
# Create Systemd file for FastCGI Wrap service and Start them.
[root@www ~]# vim /usr/lib/systemd/system/fcgiwrap.service
[Unit]
Description=Simple CGI Server
After=nss-user-lookup.target
Requires=fcgiwrap.socket
[Service]
EnvironmentFile=/etc/sysconfig/fcgiwrap
ExecStart=/usr/sbin/fcgiwrap ${DAEMON_OPTS} -c ${DAEMON_PROCS}
User=nginx
Group=nginx
[Install]
Also=fcgiwrap.socket
[root@www ~]# vim /usr/lib/systemd/system/fcgiwrap.socket
[Unit]
Description=fcgiwrap Socket
[Socket]
ListenStream=/run/fcgiwrap.socket
[Install]
WantedBy=sockets.target
[root@www ~]# systemctl enable --now fcgiwrap
# If SELinux is enabled, change policy.
[root@www ~]# vim nginx-www.te
module nginx-server 1.0;
require {
type httpd_t;
type var_run_t;
class sock_file write;
}
#============= httpd_t ==============
allow httpd_t var_run_t:sock_file write;
[root@www ~]# checkmodule -m -M -o nginx-server.mod nginx-server.te
checkmodule: loading policy configuration from nginx-server.te
checkmodule: policy configuration loaded
checkmodule: writing binary representation (version 19) to nginx-server.mod
[root@www ~]# semodule_package --outfile nginx-server.pp --module nginx-server.mod
[root@www ~]# semodule -i nginx-server.pp
测试
# 准备测试文件
# Create a test scripts with a language (example below is Python3) under the directory you set CGI executable ([/usr/share/nginx/cgi-bin] on this example) and Access to it to verify CGI works normally.
[root@www ~]# vim /usr/share/nginx/cgi-bin/index.cgi
#!/usr/bin/python3
print("Content-type: text/html\n")
print("<html>\n<body>")
print("<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: shizhaner;\">")
print("CGI Script Test Page")
print("</div>")
print("</body>\n</html>")
[root@www ~]# chmod 755 /usr/share/nginx/cgi-bin/index.cgi
# 测试
[root@client ~]# curl http://www.shizhan.cloud/cgi-bin/index.cgi
反向代理
客户端访问代理服务器,代理服务器会将客户端请求发送给真实服务器。
反向代理实现了隐藏内部服务器。
可以类比为:
正向代理: 科学上网,我知道目标位置在哪里,但是我自己无法到达。通过花钱,请能到达的人,帮我获取资料,然后再转达给我。
反向代理: 不关心目标从哪里来,我知道找谁要,他能给我提供。他自己也是找别人要的。
角色说明
-
代理服务器 proxy 10.1.8.11
-
真实服务器 www 10.1.8.10
真实服务器配置
# 安装nginx
[root@www ~]# yum -y install nginx
# 启动nginx
[root@www ~]# systemctl enable --now nginx
# 防火墙
[root@www ~]# firewall-cmd --add-service=http --permanent
[root@www ~]# firewall-cmd --reload
# 准备测试页
[root@www ~]# echo hello shizhan > /usr/share/nginx/html/test.html
代理服务器配置
windows客户端修改 C:\Windows\System32\drivers\etc\hosts
10.1.8.11 www.shizhan.cloud
10.1.8.10 shop.shizhan.cloud
# 配置解析
[root@proxy ~]# echo '10.1.8.11 www.shizhan.cloud' >> /etc/hosts
[root@proxy ~]# echo '10.1.8.10 shop.shizhan.cloud' >> /etc/hosts
# 安装 nginx
[root@proxy ~]# yum -y install nginx
# 启动 nginx
[root@proxy ~]# systemctl enable --now nginx
# 防火墙
[root@proxy ~]# firewall-cmd --add-service=http --permanent
[root@proxy ~]# firewall-cmd --reload
# 配置代理,server部分更改如下
[root@proxy ~]# vim /etc/nginx/conf.d/proxy.conf
# change [server] section like follows
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 规则
location /proxy/ {
proxy_pass http://shop.shizhan.cloud/;
}
}
[root@proxy ~]# systemctl restart nginx
# If SELnux is enabled, change boolean setting.
[root@proxy ~]# setsebool -P httpd_can_network_connect on
# 测试
[root@proxy ~]# curl http://shop.shizhan.cloud/test.html
hello shizhan
[root@proxy ~]# curl http://www.shizhan.cloud/proxy/test.html
hello shizhan
访问浏览器

反向代理案例(部署博客+电商)
实现如下目标:
访问 http://www.shizhan.cloud/shop/ -> http://shop.shizhan.cloud/
访问 http://www.shizhan.cloud/blog/ -> http://blog.shizhan.cloud/
真实服务器配置
配置后端服务器
# 配置后端服务器
[root@server]# yum install -y nginx
[root@server ~ 19:59:40]# cd /etc/nginx/conf.d
# /usr/share/nginx/shop/source/ecshop
[root@server conf.d 19:59:55]# vim vhost-shop.shizhan.cloud.conf
server {
listen 80;
listen [::]:80;
server_name shop.shizhan.cloud;
root /usr/share/nginx/shop;
# 设置默认主页
index index.php;
autoindex on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
}
[root@server conf.d 20:01:47]# vim vhost-blog.shizhan.cloud.conf
server {
listen 80;
listen [::]:80;
server_name blog.shizhan.cloud;
root /usr/share/nginx/blog;
# 设置默认主页
index index.php;
autoindex on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
}
[root@server conf.d 20:03:06]# cd /usr/share/nginx
# 备份html
[root@server nginx 20:12:51]# mv html html.ori
# 将wordpress压缩包和ECshop压缩包上传至/usr/share/nginx
[root@server nginx 20:13:05]# rz -E
rz waiting to receive.
[root@server nginx 20:13:57]# rz -E
rz waiting to receive.
[root@server nginx 20:14:03]# ls
ECShop_V4.1.20_UTF8.zip html.ori modules wordpress-4.9.4-zh_CN.zip
# 解压
[root@server nginx 20:15:57]# unzip wordpress-4.9.4-zh_CN.zip
[root@server nginx 20:16:24]# unzip ECShop_V4.1.20_UTF8.zip
# 重命名
[root@server nginx 18:02:31]# mv ECShop_V4.1.20_UTF8_release20250416/source/ecshop/ shop
[root@server nginx 18:03:14]# rm -fr ECShop_V4.1.20_UTF8_release20250416/
[root@server nginx 20:18:21]# mv wordpress blog
[root@server nginx 20:18:35]# rm -f ECShop_V4.1.20_UTF8.zip wordpress-4.9.4-zh_CN.zip
[root@server nginx 20:18:59]# ls
blog html.ori modules shop
[root@server ~]# systemctl restart nginx
配置php
[root@server ~ 20:43:33]# yum install -y php php-fpm
[root@server ~ 20:43:33]# yum install -y php-gd php-common php-pear php-mbstring php-mcrypt php-mysqlnd
[root@server ~ 20:43:33]# systemctl enable php-fpm.service --now
# 修改 php-fpm运行用户身份
[root@server ~]# vim /etc/php-fpm.d/www.conf
# 更改以下两条记录
# user = apache
user = nginx
# group = apache
group = nginx
# php 配置
[root@server ~ 20:43:33]# vim /etc/nginx/default.d/php.conf
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;
}
# 修改owner
[root@server ~]# chown nginx:nginx -R /usr/share/nginx
[root@server ~]# chown nginx:nginx -R /var/lib/php/
[root@server ~]# systemctl restart nginx php-fpm
配置数据库
# 配置blog的数据库
[root@server blog 20:29:14]# yum install -y mariadb-server
[root@server blog 20:30:23]# systemctl enable mariadb --now
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@server blog 20:32:23]# mysql_secure_installation
[root@server blog 20:34:40]# mysql -uroot -p123
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> create user wordpress identified by 'wordpress';
MariaDB [(none)]> grant all privileges on wordpress.* to wordpress;
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit
# 配置blog的数据库连接
[root@server nginx 18:08:52]# cd /usr/share/nginx/blog/
[root@server blog 20:38:21]# mv wp-config-sample.php wp-config.php
[root@server blog 20:38:55]# vim wp-config.php
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');
/** MySQL数据库用户名 */
define('DB_USER', 'wordpress');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'wordpress');
# 配置shop的数据库
[root@ecshop ~]# mysql -u root -p123
MariaDB [(none)]> CREATE DATABASE ecshop;
MariaDB [(none)]> CREATE USER ecshop@localhost IDENTIFIED BY 'redhat';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON ecshop.* TO ecshop@localhost;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit
# 重启nginx
[root@server ~ 20:53:07]# systemctl restart nginx.service
本地hosts配置
windows客户端修改 C:\Windows\System32\drivers\etc\hosts
##########博客和电商############
10.1.8.10 blog.shizhan.cloud shop.shizhan.cloud
10.1.8.11 www.shizhan.cloud www
代理服务器配置
[root@proxy ~ 20:58:35]# vim /etc/hosts
10.1.8.10 shop.shizhan.cloud blog.shizhan.cloud
[root@proxy ~ 18:13:04]# yum install -y nginx
[root@proxy ~ 21:02:33]# vim /etc/nginx/conf.d/proxy.conf
# change [server] section like follows
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.shizhan.cloud;
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 规则
location /shop/ {
proxy_pass http://shop.shizhan.cloud/;
}
location /blog/ {
proxy_pass http://blog.shizhan.cloud/;
}
}
[root@proxy ~ 21:03:20]# systemctl restart nginx
浏览器测试:
访问 http://www.shizhan.cloud/shop/ http://shop.shizhan.cloud/
访问 http://www.shizhan.cloud/blog/ http://blog.shizhan.cloud/


Ngnix+Tomcat 动静分离
动态资源和静态资源
- 静态资源:用户多次访问后,html源代码不会发生改变,例如 html、jpg 、css、js 等不需要后台处理的资源。
- 动态资源:用户多次访问后,html源代码可能会发生改变,例如我们访问的JSP页面 (本质是一个Servlet)。
- 区别:判断标准是,刷新多次,html源代码是否发生改变 。
动静分离架构

第一种:静态资源和动态资源分别部署到独立台服务器上,也是目前主流推崇的方案,大型互联网公司前端与后端是分离开发的。
第二种:静态资源和动态资源共同放到一台服务器上。此种方法非常适合不经常变动的资源。
本次演示第二种。
为什么要使用动静分离?
- 静态服务器一般使用 Nginx,Nginx实现静态服务器要比Tomcat 快得多。
- 减轻服务器的压力、提高服务器的响应速度和效率、保证高并发,与数据库的读写分离一样。
- 静态服务器我们同时也可以使用CDN做内容分发,访问不同的资源转发到不同的服务器。
最佳实践
-
安装 nginx,并启动 nginx
-
安装 tomcat,并启动 tomcat
配置 nginx,动态页面指向 tomcat
http {
......
upstream tomcat {
server www.shizhan.cloud:8080;
}
......
server {
......
# 添加代理
location /tomcat/ {
proxy_pass http://tomcat/;
}
}
}
[root@www ~]# systemctl restart nginx
web服务器本身要能够解析www.shizhan.cloud。
10.1.8.10 www.shizhan.cloud
测试
[root@client ~]# firefox http://www.shizhan.cloud
[root@client ~]# firefox http://www.shizhan.cloud/tomcat/test/index.jsp
经常变动的资源。
本次演示第二种。
### 为什么要使用动静分离?
- 静态服务器一般使用 Nginx,Nginx实现静态服务器要比Tomcat 快得多。
- 减轻服务器的压力、提高服务器的响应速度和效率、保证高并发,与数据库的读写分离一样。
- 静态服务器我们同时也可以使用CDN做内容分发,访问不同的资源转发到不同的服务器。
### 最佳实践
- 安装 nginx,并启动 nginx
- 安装 tomcat,并启动 tomcat
**==配置 nginx,动态页面指向 tomcat==**
```bash
http {
......
upstream tomcat {
server www.shizhan.cloud:8080;
}
......
server {
......
# 添加代理
location /tomcat/ {
proxy_pass http://tomcat/;
}
}
}
[root@www ~]# systemctl restart nginx
web服务器本身要能够解析www.shizhan.cloud。
10.1.8.10 www.shizhan.cloud
测试
[root@client ~]# firefox http://www.shizhan.cloud
[root@client ~]# firefox http://www.shizhan.cloud/tomcat/test/index.jsp
4097

被折叠的 条评论
为什么被折叠?



