出色先容 媒介
本文主要實現利用Nginx作為Web處事器,并利用URL Rewrite實現將手機對Web站點的請求專門重寫到一個專門為手機定制的Web頁面中。
情況先容
筆者只有一臺虛擬機, 橋接到室內的路由器便于手機舉辦會見, IP地點為192.168.1.103。
Nginx先容
engine x發音同Nginx,作者是Igor Sysoev,是今朝世界上占有率第三的Web處事器軟件. Nginx是一款輕量級的Web處事器,可實現反向署理,URL rewrite等成果。
Nginx擁有耗損內存小、可支持高并發毗連達5W個、還支持熱陳設、高機能的網絡IO模子等特性。淘寶還基于Nginx舉辦二次研發出Tengine。
編譯安裝Nginx
需要安裝Development Tools和Server Platform Development包組和zlib-devel, pcre-devel, openssl-devel等包。
[[email protected] ~]# yum groupinstall "Development Tools" "Server Platform Development" #安裝包組
[[email protected] ~]# yum install pcre-devel openssl-devel zlib-devel -y #安裝相應軟件
[[email protected] ~]# tar xf nginx-1.6.1.tar.gz -C /usr/src/ #解壓nginx源碼包到/usr/src/目次中
[[email protected] ~]# cd /usr/src/
[[email protected] src]# cd nginx-1.6.1/
[[email protected] nginx-1.6.1]# groupadd -r nginx #建設組
[[email protected] nginx-1.6.1]# useradd -r -g nginx nginx #建設用戶
[[email protected] nginx-1.6.1]# ./configure --prefix=/usr/src/nginx --sbin-path=/sbin/ --conf-path=/etc/nginx/nginx.conf --with-http_ssl_module --user=nginx --group=nginx --with-http_gzip_static_module
#關于編譯選項的參數寄義,請查閱官方文檔
[[email protected] nginx-1.6.1]# make && make install
設置文件表明
關于Nginx的一些事情道理我們這里不做表明,可是我們表明一下Nginx的設置文件中常用選項的意思
nginx的主設置文件是nginx.conf,設置文件的位置跟著編譯的設置選項而定,我們這里是/etc/nginx/nginx.conf文件
Nginx作為web處事器時主設置文件一般分為三段, main和event{},http{}、我們別離舉辦先容
main和event{}的設置
運行相關的設置
user User_Name [Group_name]; #運行Nginx歷程的用戶和組. 默認為nobody
error_log /path/to/error_log; #是否啟用錯誤日志,并指定錯誤日志的存放位置, 可指定為相對路徑
error_log /path/to/error_log notice; #指定錯誤日志的記錄的級別
pid /path/to/pidfile; #指定守護歷程pid文件的位置
機能相關的設置
worker_processes number; #運行的worker歷程的個數, 默認為1
worker_cpu_affinity cpumask ...; #界說worker歷程和cpu的綁定, 這里不做過多先容, 不相識的可自行查找
time_resolution interval ; 計數器的理會度,記錄日志時時間的準確性
worker_priority number; #worker歷程的優先級
事件相關的設置
accept_mutex on|off; #master歷程調治用戶請求至worker歷程的算法,輪詢和隨機. on暗示輪詢
use [epoll|rtsing|select|poll]; #指明利用的事件驅動模子
worker_connections number; 指明一個worker歷程可以或許接管的最大請求書
http{}的根基設置
1. server{}: 界說一個虛擬主機
示例:
server {
listen 80;
server_name www.anyisalin.com;
root "/htdocs/www"
}
2. listen
語法: listen address[:port];
示例:
listen 127.0.0.1:8000;
listen 127.0.0.1;
listen 8000;
listen *:8000;
listen localhost:8000;
3. server_name
語法: server_name name...;
支持通配符:
匹配順序:
1. 準確匹配
2. 從左向右匹配通配符 *.anyisalin.com
3. 從右向左匹配通配符 anyisalin.*
4. 匹配正則表達式 ~^*.anyisalin.com$
5. default_server
4. root
語法: root path;
5. location
語法: location [=] [~] [~*] [^~] URL {...}
成果:按照用戶請求的URI來匹配界說的location
=: 準確匹配查抄
~: 正則表達式匹配
~*: 正則表達式匹配, 不區分巨細寫
^~: URI的前半部門匹配, 不支持正則表達式
示例:
server {
listen 80;
server_name www.anyisalin.com;
location / {
root "/htdocs/www";
}
location /imgs/ {
root "/htdocs/imgs"
}
location ~* .php$ {
root "/htdocs/php"
}
}
設置Nginx 搭建一個根基的Nginx Web處事器
編輯Nginx設置文件結果如下