Nginx-DAY2

一、部署LNMP环境

1)安装软件

为了不受到之前实验的影响,可以先删除nginx,重安装

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s stop  #如果nginx没有停止,执行命令停止,如果已经停止,无需执行

[root@proxy ~]# rm -rf /usr/local/nginx/    #删除nginx原有目录

[root@proxy ~]# cd /root/lnmp_soft/nginx-1.22.1/

[root@proxy nginx-1.22.1]# yum -y install gcc make openssl-devel pcre-devel

[root@proxy nginx-1.22.1]# ./configure --with-http_ssl_module

[root@proxy nginx-1.22.1]# make && make install  #编译安装

2)安装MariaDB,php和php-fpm

[root@proxy nginx-1.22.1]# yum -y install mariadb  mariadb-server mariadb-devel php php-mysqlnd php-fpm

mariadb(数据库客户端软件)、mariadb-server(数据库服务器软件)、mariadb-devel(依赖包)、php(识别php语言)、php-fpm(进程管理器服务)、php-mysqlnd(PHP的数据库扩展包)

3)启动服务

1)启动Nginx服务

[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx

[root@proxy nginx-1.22.1]# ss -antlp | grep 80

tcp   LISTEN 0      128          0.0.0.0:80        0.0.0.0:*    users:(("nginx",pid=15507,fd=6),("nginx",pid=15506,fd=6))

2)启动MySQL服务

[root@proxy nginx-1.22.1]# systemctl enable --now mariadb  #加入开机自启并立即启动

[root@proxy nginx-1.22.1]# systemctl status mariadb  #查看服务状态

3)启动PHP-FPM服务

[root@proxy nginx-1.22.1]# systemctl enable --now php-fpm  #加入开机自启并立即启动

[root@proxy nginx-1.22.1]# systemctl status php-fpm          #查看服务状态

4)使用PHP测试页面

[root@proxy nginx-1.22.1]# cp /root/lnmp_soft/php_scripts/test.php /usr/local/nginx/html/   #拷贝动态网站测试页面到nginx中

使用浏览器访问192.168.88.5/test.php 则无法看到页面内容,而是会当成要下载的文件,因为无法解析php动态页面

二、配置动静分离

使用IP端口方式连接

通过调整Nginx服务端配置,实现以下目标:

配置Fast-CGI支持PHP网页解析

Fast-CGI是快速公共(通用)网关接口,可以连接如nginx等网站程序到网站的语言解释器(比如php) ,php-fpm进程使用了Fast-CGI解析动态网站页面

1)修改Nginx配置文件并启动服务

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf

...

 65         location ~ \.php$ {     #~是使用正则表达式匹配以.php结尾,\ 转义

 66             root           html;

 67             fastcgi_pass   127.0.0.1:9000;  #将请求转发给本机php-fpm的9000端口

 68             fastcgi_index  index.php;       #网站默认页

 69             include        fastcgi.conf;    #加载fastcgi配置文件

 70         } 

2)修改 php-fpm配置文件

打开php-fpm配置文件,注意该配置文件中;(分号)是注释

[root@proxy nginx-1.22.1]# vim /etc/php-fpm.d/www.conf

 38 listen = 127.0.0.1:9000     #更改php-fpm端口号(使用网络通信)

[root@proxy nginx-1.22.1]# systemctl restart php-fpm    #重启服务

[root@proxy nginx-1.22.1]# ss -antlp | grep 9000        #查看监听端口

LISTEN 0      128        127.0.0.1:9000      0.0.0.0:*    users:(("php-fpm",pid=15808,fd=8),("php-fpm",pid=15807,fd=8),("php-fpm",pid=15806,fd=8),("php-fpm",pid=15805,fd=8),("php-fpm",pid=15804,fd=8),("php-fpm",pid=15803,fd=6))

了解:

[root@proxy nginx-1.22.1]# vim /etc/php-fpm.d/www.conf

115 pm.max_children = 50        #最大进程数量

120 pm.start_servers = 5        #最小进程数量

3)测试

1)启动或者重加载nginx

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

再次使用浏览器访问192.168.88.5/test.php 可以看到页面内容

2)再测试连接数据库的PHP页面

可以参考lnmp_soft/php_scripts/mysql.php

[root@proxy nginx-1.22.1]# cp  /root/lnmp_soft/php_scripts/mysql.php  /usr/local/nginx/html    #拷贝动态网站测试页面到nginx中

3)客户端使用浏览器访问服务器PHP首页文档,检验是否成功

http://192.168.88.5/mysql.php       #访问成功

然后修改数据库内容进行测试

[root@proxy nginx-1.22.1]# mysql   #进入数据库

MariaDB [(none)]> create user dc@localhost identified by '123';   #创建测试账户

MariaDB [(none)]> exit  #退出

浏览器访问192.168.88.5/mysql.php 可以看到新创建的用户

使用socket方式连接

1)更改php-fpm配置

1)打开php-fpm配置文件

[root@proxy nginx-1.22.1]# vim /etc/php-fpm.d/www.conf

 38 listen = /run/php-fpm/www.sock                #socket方式(使用进程通信)

 55 listen.acl_users = apache,nginx,nobody        #添加nobody账户

[root@proxy nginx-1.22.1]# systemctl restart php-fpm    #重启服务

修改Nginx配置文件并启动服务

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf

...

 65         location ~ \.php$ {     #匹配以.php结尾

 66             root           html;

 67             fastcgi_pass   unix:/run/php-fpm/www.sock;  #将请求转发给php-fpm进程

 68             fastcgi_index  index.php;

 69             include        fastcgi.conf;        #加载fastcgi配置文件

 70         }

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

使用浏览器访问192.168.88.5/test.php 可以看到页面内容

三、地址重写

地址重写语法

关于Nginx服务器的地址重写,主要用到的配置参数是rewrite

语法格式:

rewrite  regex  replacement  flag

rewrite  旧地址   新地址    [选项]

1)修改配置文件

(访问a.html重定向到b.html)

1)修改Nginx服务配置

[root@proxy nginx-1.22.1]# cd /usr/local/nginx/

[root@proxy nginx]# cp conf/nginx.conf.default conf/nginx.conf  #还原配置文件

cp: overwrite 'conf/nginx.conf'? y

[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf

.. ..

server {

        listen       80;

        server_name  localhost;

        rewrite  /a.html  /b.html;       #新添加地址重写,a.html重定向到b.html  

        ...

    location / {

        root   html;

        index  index.html index.htm;

    }

}

[root@proxy nginx]# echo "nginx-B~~" > /usr/local/nginx/html/b.html

2)重新加载配置文件

[root@proxy nginx]# /usr/local/nginx/sbin/nginx  -s  reload

3)客户端测试

http://192.168.88.5/a.html          #内容显示的是nginx-B~~,但是地址栏没有发生变化,还是a.html页面

此时配置文件中直接写rewrite  /a.html  /b.html; 配置,在测试是其实会有些问题,比如在浏览器中访问时把192.168.88.5/a.html写成192.168.88.5/a.htmldc 或者写成 192.168.88.5/dc/a.html,访问都会正常显示b.html的页面,这是因为此时写的是只要包含a.html的都会跳转,没有进行精准匹配,可以进行以下修改,只有写a.html时才会正确跳转

[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf

.. ..

server {

        listen       80;

        server_name  localhost;

        rewrite  ^/a\.html$  /b.html;       #新添加地址重写,a.html重定向到b.html  

        ...

[root@proxy nginx]# /usr/local/nginx/sbin/nginx  -s  reload

浏览器重新访问测试即可192.168.88.5/a.html,显示b.html页面内容

2)测试重定向

#redirect  临时重定向,状态码302

#permanent 永久重定向,状态码301

1)修改Nginx服务配置

[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf

.. ..

server {

        listen       80;

        server_name  localhost;

        rewrite ^/a\.html$  /b.html  redirect;      #新修改,redirect重定向,测试完之后把redirect换成permanent,是一样的效果

        ...

    location / {

        root   html;

        index  index.html index.htm;

    }

}

2)重新加载配置文件

[root@proxy nginx]# /usr/local/nginx/sbin/nginx  -s  reload

3)浏览器测试,地址栏同时发生变化

http://192.168.88.5/a.html  #内容显示的是nginx-B~~,地址栏发生变化,是b.html页面

3)不同网站间跳转

修改Nginx服务配置实现访问192.168.88.5的请求重定向至www.tmooc.cn

[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf

.. ..

server {

        listen       80;

        server_name  localhost;

    rewrite /  http://www.tmooc.cn/;        #新修改,访问旧网站的任意内容都跳转到新网站

    location / {

        root   html;

        index  index.html index.htm;

    }

}

2)重新加载配置文件

[root@proxy nginx]# /usr/local/nginx/sbin/nginx  -s  reload

3)客户端测试

http://192.168.88.5     #可以成功跳转

4)子页面重定向

修改配置文件(访问192.168.88.5/下面子页面,重定向至www.tmooc.cn/下相同的子页面)

1) 修改Nginx服务配置

[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf

.. ..

server {

        listen       80;

        server_name  localhost;

    rewrite  /(.*)  http://www.tmooc.cn/$1;     #新修改

    location / {

        root   html;

        index  index.html index.htm;

    }

}

2)重新加载配置文件

[root@proxy nginx]# /usr/local/nginx/sbin/nginx  -s  reload

3)客户端测试

http://192.168.88.5/b.html      #成功跳转

5)实现不同浏览器跳转到不同页面

1) 创建网页目录以及对应的页面文件:

[root@proxy nginx]# mkdir  html/firefox

[root@proxy nginx]# echo  firefox~~  >  html/firefox/abc.html   #火狐专用页面

[root@proxy nginx]# echo  others~~  >  html/abc.html            #其他浏览器专用页面

火狐访问192.168.88.5/abc.html时可以看到html/firefox/abc.html里面内容

其他浏览器访问192.168.88.5/abc.html时可以看到html/abc.html里面内容

2) 修改Nginx服务配置

[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf

.. ..

server {

        listen       80;

        server_name  localhost;

        if ($http_user_agent ~* firefox) {  #如果用户使用了火狐浏览器

            rewrite /(.*)  /firefox/$1;     #就进行地址重写,让用户看到火狐专用页面,否则就是其他页面;$http_user_agent是nginx的内置变量,包含了发起 HTTP 请求的客户端的用户代理(User-Agent)字符串,比如用的什么浏览器

        }

    location / {

        root   html;

        index  index.html index.htm;

}

3)重新加载配置文件

[root@proxy nginx]# /usr/local/nginx/sbin/nginx  -s  reload

4)客户端测试

用火狐浏览器与其他浏览器访问相同地址192.168.88.5/abc.html,可以得到不同结果

火狐浏览器访问192.168.88.5/abc.html,得到结果firefox~~

其他浏览器访问192.168.88.5/abc.html,得到结果others~~

其他选项测试

#last 不再读其他语句,但还会继续匹配其他location语句

#break 不再读其他语句,结束请求

测试last不再读其他语句

1)修改Nginx服务配置

[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf

.. ..

server {

        listen       80;

        server_name  localhost;

        rewrite /a.html /b.html;        #新修改

        rewrite /b.html /c.html;        #新修改

        ...

}

...

2)重新加载配置文件

[root@proxy nginx]# /usr/local/nginx/sbin/nginx  -s  reload

[root@proxy nginx]# echo nginx-c~~ > html/c.html

3)浏览器测试

192.168.88.5/a.html #内容显示的是nginx-c~~

如果想要访问的是b.html的内容,可以做以下更改

[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf

.. ..

server {

        listen       80;

        server_name  localhost;

        rewrite /a.html /b.html last;       #新修改

        rewrite /b.html /c.html;

        ...

}

...

重新加载配置文件

[root@proxy nginx]# /usr/local/nginx/sbin/nginx  -s  reload

浏览器测试访问

192.168.88.5/a.html #内容显示的是nginx-b~~

测试last会继续匹配其他location语句

[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf

...

    location / {                #此处为默认的location

            rewrite /a.html /b.html last;   #新添加

            root   html;

            index  index.html index.htm;

        }

    location /b.html {                #这里是新添加的location

            rewrite /b.html /c.html;

        }

...        

重新加载配置文件

[root@proxy nginx]# /usr/local/nginx/sbin/nginx  -s  reload

客户端测试: http://192.168.88.5/a.html,显示为nginx-c~~

break 不再读其他语句,结束请求

[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf

...

    location / {

            rewrite /a.html /b.html break;        #break可以阻止后面的语句

            root   html;

            index  index.html index.htm;

        }

    location /b.html {

            rewrite /b.html /c.html;

        }

...        

重新加载配置文件

[root@proxy nginx]# /usr/local/nginx/sbin/nginx  -s  reload

客户端测试: http://192.168.88.5/a.html,显示为nginx-b~~

四、编写systemd Unit文件

Unit文件语法格式参考表

语句

描述

Description描述信息
After在哪个服务之后启动
Before在哪个服务之前启动
type服务类型,默认为simple
EnvironmentFile定义变量文件
ExecStart执行systemctl start需要启动的进程名称
ExecStop执行systemctl stop需要停止的进程名称
ExecReload执行systemctl reload需要执行的命令

使用systemd管理Nginx

编写Unit文件

[root@proxy ~]# cd /usr/lib/systemd/system

[root@proxy ~]# cp httpd.service nginx.service

[root@proxy ~]# vim nginx.service

[Unit]

Description=The Nginx HTTP Server       #描述信息

After=network.target remote-fs.target nss-lookup.target     #在网络程序,网络文件系统,域名解析等服务启动之后,再启动nginx   

[Service]

Type=forking     #forking多进程类型服务

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/bin/kill -s QUIT $MAINPID        #kill给程序发送QUIT退出信号,关闭nginx

[Install]

WantedBy=multi-user.target

[root@proxy ~]# systemctl start nginx    #可以控制nginx开启了,这里如果无效可以尝试重启服务器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值