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