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