本文主要内容包括
- 源码安装nginx(可以下载更高版本的nginx)
- 安装php-fpm以及配置nginx将php动态请求转发至php-fpm
- nginx做代理服务器
- nginx做负载均衡
一. 安装nginx
下载nginx安装包
wget http://nginx.org/download/nginx-1.10.3.tar.gz
安装依赖
挂载光盘, 执行
yum -y install gcc gcc-c++ openssl openssl-devel cyrus-sasl-md5 zlib
安装nginx需要指定zlib和pcre的源码位置
下载源码包wget http://zlib.net/zlib-1.2.11.tar.gz
wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
tar -zxvf pcre-8.41.tar.gz
下载openssl源码包
wget https://www.openssl.org/source/openssl-1.0.2q.tar.gz
tar -zxf openssl-1.0.2q.tar.gz安装nginx, 指定zlib和pcre的源码位置
tar -zxvf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./configure --prefix=/usr/local/nginx --with-openssl=/usr/local/src/openssl-1.0.2q --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.41 --with-zlib=/usr/local/src/zlib-1.2.11
make && make install
- 如果使用stream四层负载均衡, 加入配置
--with-stream
二. 常用配置
- 创建nginx用户
useradd -r web
- 修改配置文件,
cd /usr/local/nginx
vi ./conf/nginx.conf #./以web运行,
user web;
- 启动nginx
./sbin/nginx
- 浏览器访问, 192.168.1.132, 即能看到nginx欢迎页面
启动:sbin/nginx
立即停止:sbin/nginx –s stop
平滑停止:sbin/nginx –s quit
重载配置:sbin/nginx –s reload
重开日志:sbin/nginx –s reopen
三. nginx运行php
1.安装php, 开启php-fpm
- 获取源码包解压
wget http://cn2.php.net/get/php-7.2.1.tar.gz/from/this/mirror
#解压
mv mirror php-7.2.tar.gz
tar -zxvf php-7.2.1.tar.gz
cd php-7.2.1
- 安装依赖
yum -y install libxml2 libxml2-devel autoconf libjpeg libjpeg-devel libpng libpng-dev freetype freetype-devle zlib zlib-devel glibc glibc-devel glib2 glib2-devel libxml2-devel libcurl-devel libpng-devel freetype-devel
- 安装libmcrypt
获取源码包
https://pan.baidu.com/s/1bBfG6z9TlErw5y58eGe8Hw 密码:wlld
解压
tar jxf libmcrypt-2.5.8.tar.bz2
编译配置
cd libmcrypt-2.5.8
./configure --prefix=/usr/local/libmcrypt
编译
make
安装
make install
- 编译安装
./configure --prefix=/usr/local/php-fpm --enable-fpm --with-openssl --with-mysqli --with-pdo-mysql --enable-mbstring --with-zlib --enable-sockets --with-curl --with-pcre-regex --with-mcrypt=/usr/local/libmcrypt --with-gd --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-opcache
make && make install
# 拷贝配置文件
cp php.ini-development /usr/local/php-fpm/lib/
2. 管理PHP-FPM
PHP的FPM需要独立运行, 有自己的独立的配置文件. 等等.
默认情况下, FPM监听某个(127.0.0.1:9000)端口, 等待nginx(或者其他的web服务器)将请求转过来.
由于PHP独立运行了, 再修改PHP的配置, 就不需要重启web服务器(nginx)了, 重启PHP-FPM即可.
- fpm配置文件
cd /usr/local/php-fpm/
# 生成配置文件
cp etc/php-fpm.conf.default etc/php-fpm.conf
cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
- 修改配置文件
vi etc/php-fpm.conf 如下
daemonize = yes
vi etc/php-fpm.d/www.conf
user = web
group = web
listen = 127.0.0.1:9000
- 开启: sbin/php-fpm
3.配置Nginx将PHP请求转发给PHP-FPM
nginx的配置文件结构:
http段的server段, 就表示一台主机(虚拟主机).
在每台虚拟主机, server段中, 提供对请求脚本的解析工作:
location指令, 匹配请求的URL脚本. 以.php结尾的请求, 交给PHP-FPM处理
编辑nginx配置文件,
cd /usr/local/nginx/
vi conf/nginx.conf
将配置文件如下内容注释去掉, 以php结尾的文件, 转发到127.0.0.1:9000处理,
user web;
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#修改如下内容(重要)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
- 重新加载nginx
./sbin/nginx -s reload
- 在html下建立phpinfo.php文件, 输入
<?php
phpinfo();
- 浏览器中查看, http://192.168.1.132/phpinfo.php
- 修改php.ini, 需要重启php-fpm, 可以先kill掉, 再启动
四. 虚拟主机的配置
1. 增加虚拟主机
http中的server就是一个虚拟主机
增加server段, 就是增加虚拟主机
- 在nginx.conf配置文件中 增加如下内容
server {
listen 8080;
root /usr/local/nginx/html/shop;
server_name shop.test.com;
access_log logs/shop.log;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- 在./html/下创建shop文件夹,
mkdir html/shop
chown web:web html/shop/
vi html/shop/phpinfo.php
2. 支持pathinfo, 重写index.php规则
nginx默认不支持pathinfo.
pathinfo: URL的一部分, 请求脚本到请求参数间的部分, 称之为pathinfo.
PHP程序要处理pathinfo, 前提是web服务器支持URL的pathinfo部分才可以.
通过额外的解析pathinfo指令可以完成:
location / {
try_files $uri $uri/ /index.php?$query_string;
index index.html index.htm;
}
在 location ~ .php$ 解析段中, 增加分析pathinfo的功能
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# 增加如下两行
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
- 完整配置示例如下()
server {
listen 8888;
server_name localhost;
root /usr/local/nginx/html/shop;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#重要
try_files $uri $uri/ /index.php?$query_string;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
- 重新加载nginx
五. 代理服务器Nginx
浏览器会请求nginx服务器, nginx将请求转发给web服务器. 此时 站在浏览器的角度, nginx就代替(代理)了web服务器的功能, 称之为反向代理服务器.
测试环境
web02:nginx(192.168.1.132)
web01:nginx(192.168.1.131)
1. 配置
- nginx支持的 proxy_pass的指令, 可以完成代理转发. 直接转发Http请求数据.
演示如下: 在linux的nginx配置, 将请求抓发到别的web服务器上, 例如代理转发到web01的apache上. - 修改nginx的配置文件, 添加如下内容:
server {
# 监听8888端口
listen 8888;
# 配置域名
server_name proxy.nginx.com;
# 所有请求都转发到http://192.168.1.131:80上
location / {
proxy_pass http://192.168.1.131:80;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
}
}
- 重新加载nginx, 访问http://192.168.1.132:8888/phpinfo.php, 可以看到实际访问的是web01
- 除了最重要的proxy_pass外, 还有一些配合的指令来使用:
proxy_set_header Host $host; 请求的主机域名
proxy_set_header X-Real-IP $remote_addr; 转的目标IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 转发的目标IP
proxy_buffing off; 关闭nginx代理缓冲..
2. 通过配置转发, 帮助ajax请求获取外域数据
配置如下
server {
listen 8888;
server_name proxy.nginx.com;
location / {
// 正常处理
}
#以ajax开头的请求, 做转发
#其他正常处理
location /ajax {
proxy_set_header Host $host;
proxy_set_header x-Real-IP $remote_addr;
proxy_set_header x-Forwarded-For $proxy_add_x_forwarded_for;
# 设置转发地址
proxy_pass http://api.other.com;
}
}
六. 负载均衡配置
upstream 可以配置一个服务器集群
通过配置, 可以实现动静分离
#配置负载均衡
server {
listen 8999;
server_name lb.nginx.com;
location / {
root html;
index index.html index.htm;
# 定义规则
# 如果是html结尾, 就转发到html服务器上
# php转发到php上
# 其他, 则转发到静态资源上
# if和括号之间, 要有空格
if ($uri ~* \.html$){
# 服务器池
proxy_pass http://htmlservers;
}
if ($uri ~* \.php$){
proxy_pass http://phpservers;
}
proxy_pass http://picservers;
}
}
# 定义服务器集群
upstream htmlservers {
server 192.168.1.131:80 weight=2;
server 192.168.1.133:80 weight=1;
}
upstream phpservers {
server 192.168.1.131:80;
server 192.168.1.133:80;
}
upstream picservers {
server 192.168.1.131:80;
server 192.168.1.133:80;
}
- 配置完成, 重新加载nginx, 访问http://192.168.1.132:8999/index.php, 会获取不同的结果
- 如果一台机器挂掉, nginx会自动剔除