Nginx-LNMP、地址重写

安装nginx

[root@proxy ~]# tar -xf nginx-1.24.0.tar.gz 
[root@proxy ~]# cd nginx-1.24.0/
[root@proxy nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
[root@proxy nginx-1.24.0]# make && make install
[root@proxy nginx-1.24.0]# ls /usr/local/nginx/
conf  html  logs  sbin

安装MySQL

[root@proxy ~]# yum -y install mysql mysql-server

安装php

[root@proxy ~]# yum -y install php php-fpm php-mysqlnd

启动服务

[root@proxy ~]# /usr/local/nginx/sbin/nginx
[root@proxy ~]# ss -ntulp | grep 80
tcp   LISTEN 0      128          0.0.0.0:80        0.0.0.0:*    users:(("nginx",pid=8309,fd=6),("nginx",pid=8308,fd=6))
[root@proxy ~]# systemctl enable mysqld --now
[root@proxy ~]# ss -ntulp | grep 3306
tcp   LISTEN 0      128                *:3306             *:*    users:(("mysqld",pid=8443,fd=25))                      
[root@proxy ~]# systemctl enable php-fpm.service --now

FastCGI

一、FastCGI工作原理

二、工作流程

Web Server启动时载入FastCGI进程管理器

FastCGI进程管理器初始化,启动多个解释器进程

当客户端请求达到Web Server时,FastCGI进程管理器选择连接到一个解释器

FastCGI子进程完成处理后返回结果,将标准输出和错误信息从同一连接返回Web Server

三、FastCGI缺点

内存消耗大

因为是多进程,所以进程消耗更多的服务器内存,PHP-CGI解释器每进程消耗7至25兆内存,将这个数字乘以50或100就是很大的内存数

Nginx+PHP(FastCGI)服务器在3万并发连接下

  • 开10个Nginx进程消耗150M内存(10*15M)
  • 开64个php-cgi进程消耗1280M内存(20M*64)

四、配置FastCGI

php-fpm的配置文件可定义两种连接方式

[root@proxy ~]# vim /etc/php-fpm.d/www.conf
[www]
listen = /run/php-fpm/www.sock
或
listen = 127.0.0.1:9000

LNMP安装

一、php-fpm配置文件

[root@proxy ~]# vim /etc/php-fpm.d/www.conf
;listen = /run/php-fpm/www.sock    # 注释该行
listen = 127.0.0.1:9000            # php-fpm端口号(使用网络通信)
pm.max_children = 50               # 最大进程数量
pm.start_servers = 5               # 最小进程数量
[root@proxy ~]# systemctl restart php-fpm.service

二、修改nginx配置文件

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
... ...
        location ~ \.php$ {        # ~是使用正则表达式,匹配以.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;
        }        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;    # 加载fastcgi配置文件
        }
... ...
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

Nginx的默认访问日志文件为 /usr/local/nginx/logs/access.log

Nginx的默认错误日志文件为 /usr/local/nginx/logs/error.log

PHP默认错误日志文件为 /var/log/php-fpm/error.log

三、使用socket方式连接php-fpm

[root@proxy ~]# vim /etc/php-fpm.d/www.conf
listen = /run/php-fpm/www.sock
;listen = 127.0.0.1:9000
listen.acl_users = apache,nginx,nobody
[root@proxy ~]# systemctl restart php-fpm.service

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
... ...
        location ~ \.php$ {
            root           html;
            #fastcgi_pass   127.0.0.1:9000;
            fastcgi_pass   unix:/run/php-fpm/www.sock;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }
... ...
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

地址重写

一、什么是地址重写

获得一个来访的URL请求,然后改写成服务器可以处理的另一个URL的过程

二、地址重写的好处

缩短URL,隐藏实际路径提高安全性

易于用户记忆和键入

易于被搜索引擎收录

三、rewrite语法

rewrite基本语句

rewrite regex replacement flag

if(条件) {...}

四、应用案例

a.html --> b.html

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
... ...
    server {
        listen       80;
        server_name  localhost;
        rewrite ^/a\.html$ /b.html redirect;
.. ...
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

浏览器访问,地址栏同时发生变化

192.168.88.5/a.html

访问192.168.99.5/下面子页面,重定向至www.baidu.com/下相同的子页面

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
... ...
    server {
        listen       80;
        server_name  localhost;
        rewrite /(.*) https://baidu.com/$1;
... ...
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

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

[root@proxy ~]# mkdir /usr/local/nginx/html/firefox
[root@proxy ~]# echo firefox > /usr/local/nginx/html/firefox/abc.html
[root@proxy ~]# echo other > /usr/local/nginx/html/abc.html
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
... ...
        if ($http_user_agent ~* firefox) {
        rewrite (.*) /firefox/$1;
        }
... ...
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

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

五、地址重写的选项

redirect 临时重定向,状态码302,爬虫不更新URI

permanent 永久重定向,状态码301,爬虫更新URl

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
... ...
    server {
        listen       80;
        server_name  localhost;
        rewrite /a.html /b.html permanent;
... ...
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
[root@proxy ~]# curl 192.168.88.5/a.html
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>

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

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

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
... ...
    server {
        listen       80;
        server_name  localhost;
        rewrite /a.html /b.html;
        rewrite /b.html /c.html;
... ...
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
[root@proxy ~]# echo nginx-c~~~ > /usr/local/nginx/html/c.html
[root@proxy ~]# curl 192.168.88.5/a.html
nginx-c~~~
[root@proxy ~]# 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 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@proxy ~]# curl 192.168.88.5/a.html
ngixn-b~~~
[root@proxy ~]# curl 192.168.88.5/b.html
nginx-c~~~

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
... ...
        location / {
        rewrite /a.html /b.html last;
            root   html;
            index  index.html index.htm;
        }
        location /b.html {
        rewrite /b.html /c.html;
        }
... ...
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
[root@proxy ~]# curl 192.168.88.5/a.html
nginx-c~~~
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
... ...
        location / {
        rewrite /a.html /b.html break;
            root   html;
            index  index.html index.htm;
        }
        location /b.html {
        rewrite /b.html /c.html;
        }
... ...
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
[root@proxy ~]# curl 192.168.88.5/a.html
ngixn-b~~~
[root@proxy ~]# curl 192.168.88.5/b.html
nginx-c~~~
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值