Nginx服务搭建案例

Nginx服务

安装和启动

  1. 解压源码
tar -xvf nginx-xx.xx.xx.tar.gz
  1. 配置(模块有很多,这里以ssl为例)
# 配置安装路径,指定服务运行时使用的用户,安装HTTP SSL模块
./configure --prefix=/usr/local/nginx --user=nginx --with-http_ssl_module
  1. 编译和安装
make -j4 && make install
  1. 创建用户
user -M nginx -s /sbin/nologin
  1. 启动,关闭,重启,查看配置
# 启动
/usr/local/nginx/sbin/nginx
# 关闭 killall nginx也可行
/usr/local/nginx/sbin/nginx -s stop
# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
# 查看版本和配置
/usr/local/nginx/sbin/nginx -V

网站认证

  1. 编辑配置文件
vim /usr/local/nginx/conf/nginx.conf

server {
    ...
    auth_basic "请输入用户名和密码";  # 提示信息
    auth_basic_user_file "/usr/local/nginx/pass";  # 存放网站账户的文件
    ...
}
  1. 创建验证文件
yum install -y httpd-tools
# 第一次需要创建pass文件,所以需要加-c
htpasswd -c /usr/local/nginx/pass test
New password: # 输入密码
Re-type new password: # 再次输入密码
Adding password for user test01

# 添加第二个用户,不要加-c
htpasswd /usr/local/nginx/pass bhlu
New password: # 输入密码
Re-type new password: # 再次输入密码
Adding password for user test02
  1. 重新加载配置
/usr/local/nginx/sbin/nginx -s reload

多域名访问

  1. 编辑配置文件
vim /usr/local/nginx/conf/nginx.conf

http {
    ...
    server {
        listen  80; 
        server_name www.a.com;  # www.a.com这个域名
        root html_b;  # 网站根目录
        index index.html;  # 主页文件
    }
    
    server {
        listen  80; 
        server_name www.b.com;  # www.b.com这个域名
        root html_b;  # 网站根目录
        index index.html;  # 主页文件
    }
}
  1. 创建网站根目录和主页文件
mkdir /usr/local/nginx/{html_a,html_b}
echo "aaa" > /usr/local/nginx/html_a/index.html
echo "bbb" > /usr/local/nginx/html_b/index.html
  1. 重新加载配置
/usr/local/nginx/sbin/nginx -s reload

配置加密网站

  1. 编辑配置文件
vim /usr/local/nginx/conf/nginx.conf

server {
    listen       443 ssl;
    server_name  localhost;

    ssl_certificate      cert.pem;  # 公钥
    ssl_certificate_key  cert.key;  # 私钥

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        root   https;  # 设置网站根目录
        index  index.html index.htm;
    }
}
  1. 创建网站根目录,并创建主页文件
mkdir /usr/local/nginx/https
echo "test https~~~" > /usr/local/nginx/https/index.html
  1. 生成私钥
openssl genrsa > /usr/local/nginx/conf/cert.key
# 输出
Generating RSA private key, 2048 bit long modulus
..........................................................................................................................................................................................+++
........................+++
e is 65537 (0x10001)
  1. 根据私钥生成公钥
openssl req -x509 -key /usr/local/nginx/conf/cert.key > /usr/local/nginx/conf/cert.pem
# 输出
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn  # 国家
State or Province Name (full name) []:JiangSu   # 省份         
Locality Name (eg, city) [Default City]:WuXi  # 城市
Organization Name (eg, company) [Default Company Ltd]:test  # 公司
Organizational Unit Name (eg, section) []:test  # 部门
Common Name (eg, your name or your server's hostname) []:test-server  # 服务器名称
Email Address []:test@test.com  # 电子邮件
  1. 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
  1. 进行测试
curl -k https://www.a.com
# test https~~~

LNMP

LNMP环境

  • Llinux操作系统
  • NNginx网站服务
  • Mmariadb(mysql)数据库
  • Pphp编写动态网站的语言工具
  1. 初始化nginx服务
# 关闭之前实验的nginx,并将配置恢复默认
killall nginx
cp /usr/local/nginx/conf/nginx.conf.default /usr/local/nginx/conf/nginx.conf
cp:是否覆盖"/usr/local/nginx/conf/nginx.conf"? y
  1. 安装相关软件包
# 数据库相关,这里为了简单,使用的是mariadb,mariadb(数据库客户端)、mariadb-server(数据库服务端)、mariadb-devel(数据库开发环境依赖包)
yum install -y mariadb mariadb-server mariadb-devel
systemctl start mariadb && systemctl enable mariadb
systemctl status mariadb

# php相关,php(解释器)、php-fpm(帮助nginx解析php编写的动态网站的服务)、php-mysql(php与mysql关联的软件包)
yum install -y php php-fpm php-mysql
systemctl start php-fpm && systemctl enable php-fpm
systemctl status php-fpm
# php-fpm的配置文件在/etc/php-fpm.d/www.conf,里面包含了端口,服务端口,最小守护进程数量等,一个服务进程大概是25M左右
  1. 准备动态网站页面
cp /data/test.php /usr/local/nginx/html/
  1. 启动nginx服务,设置相关配置
# 启动
/usr/local/nginx/sbin/nginx

# 编辑配置文件
vim /usr/local/nginx/conf/nginx.conf
...
# 匹配以.php结尾的
location ~ \.php$ {
    root           html;  # 网站位置
    fastcgi_pass   127.0.0.1:9000;  # 找本地9000端口
    fastcgi_index  index.php;  # 默认主页
    # fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  # 这行无用,需要注释
    include        fastcgi.conf;  # 这里需要修改,使用了是另一个配置文件
}
...

# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload

地址重写

格式:rewrite 匹配路径 实际路径 选项

  • 选项
    • redirect:临时重定向,302
    • permanent:永久重定向,301
    • last:不再读其他rewrite还是除rewrite之外的其他语句的
    • break:不再读其他语句
  1. 访问a.html实际显示b.html
vim /usr/local/nginx/conf/nginx.conf

...
rewrite ^/a\.html$ /b.html;
...

# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
  1. 访问本地的任意页面,跳转到指定网站的同页面
vim /usr/local/nginx/conf/nginx.conf

...
rewrite /(.*) http://www.test.com/$1;
...

# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
  1. 根据浏览器的不同访问不同的页面
vim /usr/local/nginx/conf/nginx.conf

...
# $http_user_agent是nginx内置变量,存储了用户的信息
if ($http_user_agent ~* firefox){
    rewrite /(.*) /firefox/$1;
}
...

# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
  1. last选项
# last
vim /usr/local/nginx/conf/nginx.conf

...
server {
    ...
    location / {
        rewrite /a.html /b.html last;  # 这里使用last,不再读其他的rewrite
        ...
    }
    rewrite /b.html /c.html;
}
...

# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload

# 测试
curl 127.0.0.1/a.html  # 结果是b.html的页面
  1. break选项
# last
vim /usr/local/nginx/conf/nginx.conf

...
server {
    ...
    location / {
        rewrite /a.html /b.html break;  # 这里如果使用的是last,那么访问a.html,会显示c.html的内容,所以这里需要使用break
        ...
    }
    
    location /b.html {
        rewrite /b.html /c.html;
    }
}
...

# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload

# 测试
curl 127.0.0.1/a.html  # 结果是b.html的页面

代理功能(网站业务)

环境

  • proxy:192.168.3.1
  • web1:192.168.3.100
  • web2:192.168.3.200
  1. 开启web1web2的网站服务(这里使用httpd服务)
# web1和web2
yum install -y httpd
systemctl start httpd

# web1
echo "web1" > /var/www/html/index.html

# web2
echo "web2" > /var/www/html/index.html
  1. proxy主机上使用nginx的代理功能
# 将之前的nginx服务删除
killall nginx
rm -rf /usr/local/nginx

# 安装nginx
tar -xvf nginx-1.17.6.tar.gz
cd nginx-1.17.6/
./configure
make && make install

# 编辑配置文件
vim /usr/local/nginx/conf/nginx.conf
...
http {
    ...
    # 创建集群,集群名称叫做web
    upstream web {
        server 192.168.3.100:80;
        server 192.168.3.200:80;
    }
    server {
        listen 80;
        ...
        localtion / {
            proxy_pass http://web;  # 调用集群
            root html;
            index index.html index.htm;
        }
        ...
    }
    ...
}
...

# 启动nginx
/usr/local/nginx/sbin/nginx
  1. 测试是否成功
for i in {1..5}; do curl 192.168.3.1; done
# web1 web2 web1 web2 web1
  1. 集群优化,设置权重,配置健康检查,相同客户机访问相同服务器
# 编辑配置文件
vim /usr/local/nginx/conf/nginx.conf
...
http {
    ...
    # 创建集群,集群名称叫做web
    upstream web {
        server 192.168.3.100:80;
        # weight=2: 权重2,默认权重是1
        # max_fails=2: 检测两次如果都失败,则判为故障
        # fail_timeout=30: 故障机器30s再次测试
        server 192.168.3.200:80 weight=2 max_fails=2 fail_timeout=30;;
    }
    server {
        listen 80;
        ...
        localtion / {
            proxy_pass http://web;  # 调用集群
            root html;
            index index.html index.htm;
        }
        ...
    }
    ...
}
...

# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
  1. 再次测试
for i in {1..5}; do curl 192.168.3.1; done
# web1 web2 web2 web1 web2

如果想要将集群中某个主机不参与集群活动

server 192.168.3.200 down;即可

四层代理(其他业务)

环境

  • proxy:192.168.3.1
  • server1:192.168.3.100
  • server2:192.168.3.200
  1. 将nginx恢复默认配置
/usr/local/nginx/sbin/nginx -s stop
cp /usr/local/nginx/conf/nginx.conf.default /usr/local/nginx/conf/nginx.conf
cp:是否覆盖"/usr/local/nginx/conf/nginx.conf"? y
  1. 重新配置编译,添加四层代理模块
# 先查看之前nginx编译所带模块
/usr/local/nginx/sbin/nginx -V
# 因为上个实验没有加模块,所以最后一行只有configure arguments: 

cd ~/nginx-1.17.6/
./configure --with-stream  # --with-stream 四层代理,如果之前的nginx有模块,需要在后面加上去
make  # 注意这里只需编译即可,不需要安装

# 检查新编译的nginx是否符合要求
./objs/nginx -V
# 最后一行: configure arguments: --with-stream

# 将新编译的nginx拷贝过去
cp ./objs/nginx /usr/local/nginx/sbin/nginx 
cp:是否覆盖"/usr/local/nginx/sbin/nginx"? y

# 启动nginx试试
/usr/local/nginx/sbin/nginx
  1. 配置四层代理
vim /usr/local/nginx/conf/nginx.conf

...
# 新业务,跟http同级
stream {
    # 新集群,叫backend,这里以集群的sshd服务作为示例
    upstream backend {
        server 192.168.3.100:22;
        server 192.168.3.200:22;
    }

    server {
        listen 10022;  # 监听端口号,访问这个端口号,就会跳转到集群中
        proxy_pass backend;  # 调用集群
    }
}

http {
    ...
}

# 加载配置,如果安装的nginx没有加--with-stream,这里会报错
/usr/local/nginx/sbin/nginx -s reload
  1. 测试是否成功
ssh -p 10022 root@192.168.3.1
# 进入server1

ssh -p 10022 root@192.168.3.1
# 进入server2

常见的nginx问题

  1. 配置404报错
vim /usr/local/nginx/conf/nginx.conf

...
http {
    ...
    server {
        ...
        error_page  404              /404.html;  # 设置404页面
        ...
    }
}
...
  1. 查看网站后台数据
# 需要添加一个新的模块--with-http_stub_status_module
# 查看现在的nginx
killall nginx
/usr/local/nginx/sbin/nginx -V
# nginx version: nginx/1.17.6
# built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
# configure arguments: --with-stream

# 重新编译并拷贝过去
cd ~/nginx-1.17.6/
./configure --with-stream --with-http_stub_status_module  # 添加上之前的模块
make
cp ./objs/nginx /usr/local/nginx/sbin/nginx 
cp:是否覆盖"/usr/local/nginx/sbin/nginx"? y

# 编辑配置文件
vim /usr/local/nginx/conf/nginx.conf
...
http {
    ...
    server {
        ...
        location / {
            root   html;
            index  index.html index.htm;
        }
        
        # 添加下面几行
        location /status {
            stub_status on;  # 显示后台的数据
            allow 192.168.3.1;  # 只允许192.168.3.1查看
            deny all;  # 拒绝其他
        }
        ...
    }
}
...

# 启动nginx
/usr/local/nginx/sbin/nginx

# 本机测试查看,其他机器访问会报错403
curl 192.168.3.1/status
: << 'EOF'
Active connections: 1 
server accepts handled requests
         14       14      14 
Reading: 0 Writing: 1 Waiting: 0

Active connections: 当前活动的连接数量,即当前有多少用户访问该网站
accepts: 已接收客户端的连接总数
handled: 已处理客户端的连接总数
requests: 客户端发送的请求数
Reading: 当前服务器正在读取客户端请求头的数量
Writing: 当前服务器正在写响应信息的数量
Waiting: 当前多少客户端在等待服务器的响应

EOF

相关推荐

  1. Nginx服务案例

    2024-04-10 04:14:03       27 阅读
  2. 如何快速nginx服务

    2024-04-10 04:14:03       24 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-10 04:14:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-10 04:14:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-10 04:14:03       87 阅读
  4. Python语言-面向对象

    2024-04-10 04:14:03       96 阅读

热门阅读

  1. [lesson12]经典问题解析一

    2024-04-10 04:14:03       32 阅读
  2. 计算机网络---第二天

    2024-04-10 04:14:03       27 阅读
  3. C语言题目:阶乘数列求和(函数)

    2024-04-10 04:14:03       31 阅读
  4. Element-plus使用中遇到的问题

    2024-04-10 04:14:03       34 阅读
  5. UVA1595 Symmetry 对称轴 解题报告

    2024-04-10 04:14:03       33 阅读
  6. npm install 太慢?解决方法

    2024-04-10 04:14:03       32 阅读
  7. 父子组件传值,子组件反显问题

    2024-04-10 04:14:03       30 阅读
  8. 选择排序-c++

    2024-04-10 04:14:03       38 阅读
  9. Linux从入门到精通 --- 1.初始Linux

    2024-04-10 04:14:03       40 阅读