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~~~
相关推荐
程序无bug15 分钟前
手写Spring框架
java·后端
程序无bug17 分钟前
Spring 面向切面编程AOP 详细讲解
java·前端
全干engineer29 分钟前
Spring Boot 实现主表+明细表 Excel 导出(EasyPOI 实战)
java·spring boot·后端·excel·easypoi·excel导出
Fireworkitte38 分钟前
Java 中导出包含多个 Sheet 的 Excel 文件
java·开发语言·excel
szxinmai主板定制专家1 小时前
【精密测量】基于ARM+FPGA的多路光栅信号采集方案
服务器·arm开发·人工智能·嵌入式硬件·fpga开发
GodKeyNet1 小时前
设计模式-责任链模式
java·设计模式·责任链模式
你不知道我是谁?1 小时前
负载均衡--四层、七层负载均衡的区别
运维·服务器·负载均衡
a_Dragon11 小时前
Spring Boot多环境开发-Profiles
java·spring boot·后端·intellij-idea
泽02021 小时前
C++之红黑树认识与实现
java·c++·rpc
dyj0951 小时前
【Rancher Server + Kubernets】- Nginx-ingress日志持久化至宿主机
运维·nginx·rancher