1. LNMP 先安装nginx
yum -y install gcc openssl-devel pcre-devel
wget http://nginx.org/download/nginx-1.12.2.tar.gz (也可配置阿里源用yum安装)
tar -xvf nginx-1.12.2.tar.gz
./configure
make && make install
安装MariaDB php和php-fpm
yum -y install mariadb mariadb-server mariadb-devel php php-mysql php-fpm
/usr/local/nginx/sbin/nginx //启动Nginx服务
systemctl start mariadb //启动服务器并设开机自启
systemctl start php-fpm //启动服务并设开机自启
修改Nginx配置文件并启动服务 开启php功能
vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm;
#设置默认首页为index.php,当用户在浏览器地址栏中只写域名或IP,不说访问什么页面时,服务器会把默认首页index.php返回给用户
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000; #将请求转发给本机9000端口,PHP解释器
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf; #加载其他配置文件(改)
}
/usr/local/nginx/sbin/nginx -s reload 重启nginx
2. 地址重写
关于Nginx服务器的地址重写,主要用到的配置参数是rewrite
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
rewrite /a.html /b.html; 访问a.html转发到b.html 但域名还是a.html rewrite /a.html /b.html redirect; 多在后面加个redirect 地址转发后域名也会变成b.html
location / {
root html;
index index.html index.htm;
}
}
echo "BB" > /usr/local/nginx/html/b.html 在b.html里写入数据
/usr/local/nginx/sbin/nginx -s reload 重启nginx
firefox http://192.168.4.5/a.html 客户端测试
3. 修改配置文件(访问本机IP 192.168.17.10的请求重定向至www.tmooc.cn)
以上面配置文件为例把 rewrite后面改为以下条件即可:
rewrite ^/ http://www.tmooc.cn/;
/usr/local/nginx/sbin/nginx -s reload 重新加载配置文件
firefox http://192.168.4.5 客户端测试
4. 修改配置文件(访问192.168.17.10/下面子页面,重定向至www.tmooc.cn/下相同的页面)
以上面配置文件为例把 rewrite后面改为以下条件即可:
rewrite ^/(.*)$ http://www.tmooc.cn/$1;
/usr/local/nginx/sbin/nginx -s reload 重新加载配置文件
firefox http://192.168.4.5 firefox http://192.168.4.5/test 客户端测试
5. 修改配置文件(实现curl和火狐访问相同链接返回的页面不同)
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
#这里,~符号代表正则匹配,*符号代表不区分大小写
if ($http_user_agent ~* firefox) { //识别客户端firefox浏览器
rewrite ^(.*)$ /firefox/$1;
}
}
创建网页目录以及对应的页面文件:
echo "I am Normal page" > /usr/local/nginx/html/test.html
mkdir -p /usr/local/nginx/html/firefox/
echo "firefox page" > /usr/local/nginx/html/firefox/test.html
/usr/local/nginx/sbin/nginx -s reload 重新加载配置文件
firefox http://192.168.4.5/test.html 测试
curl http://192.168.4.5/test.html