页面是html,mp3,mp4,txt,doc,pdf
动态脚本语言:shell,PHP,java
在192.168.4.5
软件包列表:LNMP
nginx[web服务,接收用户的请求]
php [解释器]
yum -y localinstall php-fpm-5.4.16
mariadb [数据库客户端]
mariadb-server [数据库服务器]
mariadb-devel [依赖包]
php-mysql [php连接mysql的扩展包]
启动所有服务:
nginx 启动[80]
systemctl start php-fpm 启动[9000]
systemctl start mariadb 启动[3306]
netstat -nutlp |grep 80 | 9000 |3306
nginx[静态]
root html
nginx[动态]
PHP:9000
判断,如果用户访问的是静态页面,则找到,直接返回
如果用户访问的是动态页面,则转发给9000
location / {
root html;
}
location ~ \.php$
root html;
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
vim /usr/local/nginx/html/test.php
phpinfo();
?>
常见问题:
1.出现下载页面 【配置文件中没有php转发】
2.File not found. 【转发设置不正确】[SELinux]
3.An error occurred.
【转发给9000后,无响应,php未启动】
后台静态页面:
# vim /usr/local/nginx/conf/nginx.conf
server{
#admin静态页面
listen 80;
server_name admin.abc.xxxxxx.com;
access_log logs/frontaccess.log main;
error_log logs/fronterror.log;
location ~ \.(html|js|css|png|jpg|gif|woff|woff2|ttf|apk|ipa)$ {
root /usr/local/html/dist/;
index index.html index.htm;
}
location ^~ /api/
{
proxy_pass http://127.0.0.1:10001/;
}
}
案例1:访问a.html跳转到b.html
vim /usr/local/nginx/conf/nginx.conf
... ...
server {
listen 80
server_name localhost;
location / {
rewrite a.html /b.html redirect;
}
#echo "BB" > /usr/local/nginx/html/b.html
#nginx -s reload
案例2:访问192.168.4.5跳转到www.tmooc.cn
vim /usr/local/nginx/conf/nginx.conf
... ...
server {
listen 80
server_name localhost;
location / {
rewrite ^/ http://www.tmooc.cn;
}
附加:
访问旧的网站页面,跳转到新的网站相同页面
rewrite ^/(.*) http://www.jd.com/$1;
保留和粘贴
案例2:不同浏览器访问相同页面返回结果不同
ie http://192.168.4.5/test.html
firefox http://192.168.4.5/test.html
uc http://192.168.4.5/test.html
nginx【内置变量】
vim /usr/local/nginx/conf/nginx.conf
server {
... ...
if ($http_user_agent ~* curl){ // 识别客户端curl浏览器
rewrite ^/(.*) /curl/$1;
}
#cd /usr/local/nginx/html
#echo "1" >test.html
#mkdir curl
#echo "2" >curl/test.html
#nginx -s reload
firefox http://192.168.4.5/test.html
curl http://192.168.4.5/test.html
案例:如果用户访问的页面不存则转到首页
vim /usr/local/nginx/conf/nginx.conf
server {
... ...
if (!-e $request_filename){
rewrite ^/ http://192.168.4.5;
}
#nginx -s reload
rewrite 正则 URL [选项]
rewrite选项:
last 停止执行其他rewrite
break 停止执行其他rewrite,并结束请求
redirect 临时重定向
permament 永久重定向
PC端和手机端做适配:
# vim /usr/local/nginx/conf/nginx.conf
server{
listen 80;
server_name www.xxxxx.com;
access_log logs/indexaccess.log main;
error_log logs/indexerror.log;
location / {
root /usr/local/html/dist/static/index/pc/;
if ( $http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(NEC-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" )
{
root /usr/local/html/dist/static/index/mobile/;
}
index index.html index.htm;
}
}
# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name back.xxxxxx.com;
access_log logs/backaccess.log main;
error_log logs/backerror.log;
location / {
proxy_http_version 1.1;
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://127.0.0.1:10001;
}
}