linux服務(wù)器下LNMP安裝與配置方法
Nginx與apache、lighttp性能綜合對比,如下圖:
注意:關(guān)閉rpm默認(rèn)安裝的apache和mysql
1.準(zhǔn)備php函數(shù)的rpm包
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
2.準(zhǔn)備lnmp其他的源代碼包
wget http://blog.s135.com/soft/linux/nginx_php/nginx/nginx-0.8.46.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/php/php-5.2.14.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.14-fpm-0.5.14.diff.gz
wget http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.5.3-m3.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.1.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz
wget http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/pcre/pcre-8.10.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.6.1.tar.bz2
wget http://blog.s135.com/soft/linux/nginx_php/pdo/PDO_MYSQL-1.0.2.tgz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/imagick-2.3.0.tgz
3.安裝php-5.2.14源代碼包所需要的函數(shù)支持包
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/usr/local
make
make install
cd ../
(libiconv庫為需要做轉(zhuǎn)換的應(yīng)用提供了一個iconv()的函數(shù),以實現(xiàn)一個字符編碼到另一個字符編碼的轉(zhuǎn)換)
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../
(libmcrypt是加密算法擴(kuò)展庫。支持DES, 3DES, RIJNDAEL, Twofish, IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法。)
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
cd ../
(加密算法支持)
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
./configure
make
make install
cd ../
4. 編譯安裝MySQL 5.5.3-m3
groupadd mysql
useradd -g mysql mysql
tar zxvf mysql-5.5.3-m3.tar.gz
cd mysql-5.5.3-m3
./configure --prefix=/usr/local/mysql --without-debug --enable-thread-safe-client --with-pthread --enable-assembler --enable-profiling --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static --with-extra-charsets=all --with-plugins=all --with-mysqld-user=mysql --without-embedded-server --with-server-suffix=-community --with-unix-socket-path=/tmp/mysql.sock
Make
#編譯
Make install
#安裝
Cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
#準(zhǔn)備mysql配置文件
Vi /etc/my.cnf
[client]
default-character-set=utf8
#修改客戶端和連接字符集
[mysqld]
character-set-server=utf8
#修改服務(wù)器和數(shù)據(jù)庫字符集
collation-server = utf8_general_ci
#修改服務(wù)器校驗字符集 登陸mysql后可以\s查看字符集
Setfacl -m u:mysql:rwx -R /usr/local/mysql
Setfacl -m d:u:mysql:rwx -R /usr/local/mysql
#設(shè)置權(quán)限
/usr/local/mysql/bin/mysql_install_db --user=mysql
#安裝mysql和test數(shù)據(jù)庫
/usr/local/mysql/bin/mysqld_safe --user=mysql &
#啟動mysql服務(wù)
/usr/local/mysql/bin/mysqladmin -uroot password 123
#修改mysql登錄密碼為123
/usr/local/mysql/bin/mysql -uroot -p123
#用mysql登錄
5. 編譯安裝PHP(FastCGI模式。使用fastCGI管理php,加快php解析速度)
tar zxvf php-5.2.14.tar.gz
gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1
#-p 1 是數(shù)字
#解壓并打補(bǔ)丁,讓php支持fpm來方便管理php-cgi進(jìn)程(使用php-fpm管理fastCGI)
# gzip -c 保留源文件 -d 解壓
cd php-5.2.14/
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap
make ZEND_EXTRA_LIBS='-liconv'
#編譯過程設(shè)定變量(編譯過程需要)
make install
cp /lnmp/php-5.2.14/php.ini-dist /usr/local/php/etc/php.ini
cd ../
6.準(zhǔn)備編譯安裝PHP5擴(kuò)展模塊
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../
tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../
(eAccelerator是一個自由開放源碼php加速器,優(yōu)化和動態(tài)內(nèi)容緩存,提高了php腳本的緩存性能,使得PHP腳本在編譯的狀態(tài)下,對服務(wù)器的開銷幾乎完全消除。 它還有對腳本起優(yōu)化作用,以加快其執(zhí)行效率。使您的PHP程序代碼執(zhí)效率能提高1-10倍)
tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
make
make install
cd ../
tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
make
make install
cd ../
(ImageMagick是一套功能強(qiáng)大、穩(wěn)定而且免費(fèi)的工具集和開發(fā)包,可以用來讀、寫和處理超過89種基本格式的圖片文件,包括流行的TIFF、JPEG、GIF、 PNG、PDF以及PhotoCD等格式)
tar zxvf imagick-2.3.0.tgz
cd imagick-2.3.0/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../
7. 修改php.ini文件,讓php模塊生效
vi /usr/local/php/etc/php.ini
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
手工添加
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"
再查找output_buffering = Off
修改為output_buffering = On
再查找 ; cgi.fix_pathinfo=0
修改為cgi.fix_pathinfo=0,防止Nginx文件類型錯誤解析漏洞
8. 在php.ini中配置eAccelerator加速PHP
mkdir -p /usr/local/eaccelerator_cache
#準(zhǔn)備eaccelerator緩存目錄
vi /usr/local/php/etc/php.ini
[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
9.準(zhǔn)備php-cgi和nginx進(jìn)程執(zhí)行者用戶
useradd nginx
10. 創(chuàng)建php-fpm配置文件- php-fpm.conf
vi /usr/local/php/etc/php-fpm.conf
<value name="display_errors">0</value>
#0改成1,頁面上會輸出錯誤日志. 取消注釋
unix user of processes
<value name="user">nginx</value>
Unix group of processes
<value name="group">nginx</value> 取消注釋
<value name="max_children">128</value>
#最大子進(jìn)程數(shù)128,如果內(nèi)存小于2G,則64個最佳
<value name="rlimit_files">65535</value>
# Set open file desc rlimit,同時打開的文件數(shù),linux系統(tǒng)允許同時打開的文件數(shù)為1024,修改linux系統(tǒng)中允許同時打開的文件,ulimit -SHn 65535,而且這個參數(shù)重啟后還能生效,加到 /etc/profile全局配置文件的最后,開機(jī)就會生效,ulimit -a查看open files 65535
ulimit 用戶控制shell啟動進(jìn)程所占用的資源
-H 設(shè)定硬性資源限制,也就是管理員設(shè)定的限制
-S 設(shè)定軟性資源限制,彈性限制
-n 設(shè)定可同時打開的最大文件個數(shù)
-f 設(shè)定單個文件最大大小
-a 查看目前的限制
<value name="max_requests">1024</value>
#最大請求數(shù), How much requests each process should execute before respawn.一個子進(jìn)程能夠回應(yīng)1042個請求
11. 啟動php-cgi(fastcgi)進(jìn)程,127.0.0.1的9000端口,進(jìn)程數(shù)為128(如果服務(wù)器內(nèi)存小于3GB,可以只開啟64個進(jìn)程),用戶為nginx:
/usr/local/php/sbin/php-fpm start
#啟動php-cgi
/usr/local/php/sbin/php-fpm reload
#重新加載配置文件
/usr/local/php/sbin/php-fpm stop
#關(guān)閉php-fpm,此時nginx肯定連不上php
12. 安裝Nginx所需的pcre庫
tar zxvf pcre-8.10.tar.gz
cd pcre-8.10/
./configure
make && make install
cd ../
13. 安裝Nginx
tar zxvf nginx-0.8.46.tar.gz
cd nginx-0.8.46/
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../
14. 修改Nginx配置文件
vi /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;
#相當(dāng)于cpu個數(shù)
error_log logs/nginx_error.log;
#錯誤日志
pid /usr/local/nginx/nginx.pid;
#主進(jìn)程PID保存文件
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
#文件描述符數(shù)量
events
{
use epoll;
#網(wǎng)絡(luò)I/O模型,建議linux使用epoll,F(xiàn)reeBSD使用kqueue
worker_connections 65535;
#最大允許連接數(shù)
}
http
{
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#日志格式
access_log logs/access.log main;
#調(diào)用格式的日志
sendfile on;
tcp_nopush on;
#tcp延遲
keepalive_timeout 65;
#保持連接時間
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#fastcgi設(shè)置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#網(wǎng)絡(luò)壓縮設(shè)置
#limit_zone crawler $binary_remote_addr 10m;
server
{
listen 80;
server_name 192.168.150.253;
#主機(jī)名,或IP。如果是主機(jī)名,要能夠DNS解析
location / {
root html;
#網(wǎng)站主目錄。/usr/local/nginx/html/
index index.html index.htm index.php;
#默認(rèn)網(wǎng)頁順序
}
#limit_conn crawler 20;
location ~ .*\.(php|php5)?$
#~:匹配 后面正則表達(dá)式:.*任意字符 \.點(diǎn) php或php5結(jié)尾。碰到網(wǎng)頁文
件名是.php或.php5結(jié)尾
{
root html;
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
#連接fastcgi,用來解析php語句
fastcgi_index index.php;
#首頁為index.php
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#啟動fast-cgi,可以在每個服務(wù)中啟動,也可以放入/usr/local/nginx/conf/fastcgi_params,每個server都可以享用
include fastcgi_params;
#包括fastcgi_params中參數(shù)
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
#圖片格式緩存30天
}
location ~ .*\.(js|css)?$
{
expires 1h;
#js/css緩存2小時
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /data1/logs/access.log access;
}
}
15. 在/usr/local/nginx/conf/目錄中創(chuàng)建fastcgi_params文件
Vi /usr/local/nginx/conf/fastcgi_params (與配置文件中,只寫一個就好)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#建議把fastcgi_param寫到nginx.conf中而不是把它寫到fastcgi_params配置文件中,否則配置不夠靈活,比如后面默認(rèn)php設(shè)置和alias php設(shè)置中,他們的php頁面的系統(tǒng)地址是不同的,比如:
默認(rèn)php文件->/usr/local/nginx/html/index.php
Alias php文件->/mnt/bbs/index.php
這個時候你會發(fā)現(xiàn)fastcgi_params中的SCRIPT_FILENAME的值是相同的,這樣會導(dǎo)致alias php的頁面出不來,而配置在nginx.conf中各自配置各自的php系統(tǒng)地址,這樣比較靈活.
#如果你覺得每個連接php的配置中都要加這一句話有點(diǎn)冗余,那就把它加入到fastcgi_params文件中,這樣只需要加一次,其他所有的nginx.conf中的有關(guān)連接fastcgi的一塊就不用加fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name這一句話了.
16.配置開機(jī)啟動nginx,php-fpm,ulimit
1)nginx
Vi /etc/rc.local
/usr/local/nginx/sbin/nginx
2)php-fpm
Vi /etc/rc.local
/usr/local/php/sbin/php-fpm start
3)ulimit
Vi /etc/profile
ulimit -SHn 65535
4)mysql
Vi /etc/rc.local
/usr/local/mysql/bin/mysqld_safe --user=mysql &
17.檢查nginx配置文件語句錯誤
/usr/local/nginx/sbin/nginx -t
18.平滑重啟nginx進(jìn)程
1)Pkill -HUP nginx
2)kill -HUP `pgrep -uroot nginx`
Pgrep -uroot nginx 取出nginx主進(jìn)程PID
3)/usr/local/nginx/sbin/nginx -s reload
19. 編寫每天定時切割Nginx日志的腳本
1、創(chuàng)建腳本/usr/local/nginx/sbin/cut_nginx_log.sh
vi /usr/local/nginx/sbin/cut_nginx_log.sh
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/nginx/logs/"
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/nginx.pid`
2、設(shè)置crontab,每天凌晨00:00切割nginx訪問日志
crontab -e
00 00 * * * /bin/bash /usr/local/nginx/sbin/cut_nginx_log.sh
20.配置nginx虛擬主機(jī)
Sina和sohu域名事先解析
Vi /usr/local/nginx/conf/nginx.conf
==èwww.sina.com公司網(wǎng)站
server {
listen 80;
server_name www.sina.com;
access_log logs/sina.access.log main;
location / {
root /web/sina;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /web/sina;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
==èwww.sohu.com公司網(wǎng)站
server {
listen 80;
server_name www.sohu.com;
access_log logs/sohu.access.log main;
location / {
root /web/sohu;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /web/sohu;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
最后在客戶端測試虛擬主機(jī)www.baidu.com和www.sina.com兩家公司網(wǎng)站
21.列表頁顯示
location / {
autoindex on; #打開列表頁
root html;
index index.html index.php index.htm;
}
22.虛擬目錄設(shè)置
location /bbs{
alias /mnt/bbs/;
}
#這樣配置html靜態(tài)文件是可以出來的,但是php動態(tài)頁面出不來,而且會瀏覽器的頁面上會顯示" No input file specified. "的報錯,其實是php系統(tǒng)文件地址( SCRIPT_FILENAME)找不到,也就是說fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;中的$document_root$fastcgi_script_name不是真正的/mnt/bbs/index.php的地址,這可怎么解決:
location /bbs {
alias /mnt/bbs/;
index bbs.php index.html index.php;
}
location ~ ^/bbs/ {
root /mnt/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
log_format bbs '$document_root$fastcgi_script_name ';
access_log logs/bbs.access.log bbs;
}
#后面兩行是關(guān)于日志的,就是為了更好的觀察由nginx提交給fastcgi的php的系統(tǒng)地址SCRIPT_FILENAME,在這里我用$request_filename來給SCRIPT_FILENAME賦值,在日志中的結(jié)果為/mnt/bbs/index.php,在這里我發(fā)現(xiàn)一個問題就是$request_filename中的root設(shè)置為/mnt,否則$request_filename的值為:/mnt/bbs/bbs/index.php.
由以上可以得到一個結(jié)論,就是默認(rèn)php設(shè)置也可以這樣設(shè)置關(guān)于SCRIPT_FILENAME:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
log_format php '$document_root$fastcgi_script_name ';
access_log logs/php.access.log php;
}
#此時從日志中可以看到,$request_filename的值為/usr/local/nginx/html/index.php,而以前默認(rèn)的/scripts$fastcgi_script_name顯然是錯的php系統(tǒng)地址,日志中顯示為/scripts/index.php
23.nginx狀態(tài)監(jiān)控
location /nginxstatus{
stub_status on;
access_log off;
}
#客戶端訪問網(wǎng)址:http://www.baidu.com/nginxstatus
24.rewrite正則過濾
location ~ \.php$ {
proxy_pass http://127.0.0.1;
}
Rewrite指令最后一項參數(shù)為flag標(biāo)記,支持的flag標(biāo)記如下:
Last 標(biāo)示完成rewrite規(guī)則
Break 不再匹配后面的規(guī)則
Redirect 302臨時重定向
Permanent 301永久重定向
Last和break用來實現(xiàn)uri重寫,瀏覽器地址欄的url地址不變,但在服務(wù)器訪問的路徑發(fā)生了變化,redirect和permanent用來實現(xiàn)url跳轉(zhuǎn),瀏覽器地址欄會顯示跳轉(zhuǎn)后的url地址,使用alias指令時必須使用last標(biāo)記,使用proxy_pass指令時要使用break標(biāo)記,last標(biāo)記在本條rewrite規(guī)則執(zhí)行完畢后,會對其所在的server{}標(biāo)簽重新發(fā)起請求,而break標(biāo)記則在本條規(guī)則匹配完成后,終止匹配,不再匹配后面的規(guī)則.
在匹配的過程中,nginx將首先匹配字符串,然后再匹配正則表達(dá)式,匹配到第一個正則表達(dá)式后,會停止搜索,如果匹配到正則表達(dá)式,則使用正則表達(dá)式的搜索結(jié)果,如果沒有匹配到正則表達(dá)式,則使用字符串的搜索結(jié)果.
可以使用前綴"^~"來禁止匹配到字符串后,再去檢查正則表達(dá)式,匹配到url后,將停止查詢.
使用前綴"="可以進(jìn)行精確的url匹配,如果找到匹配的uri,則停止查詢,例如"location=/",只能匹配到"/",而"/test.html"則不能被匹配.
正則表達(dá)式的匹配,按照它們在配置文件中的順序進(jìn)行,寫在前面的優(yōu)先.
Location = / {
#僅僅匹配 /
[configuration A]
}
Location / {
#匹配任何以/開頭的查詢,但是正則表達(dá)式及較長的字符串(/bbs/)將被優(yōu)先匹配.
[configuration B]
}
Location ^~ /images/ {
#匹配任何以/images/開頭的字符串,并且停止搜索,所以正則表達(dá)式將不會被檢查.
[configuration C]
}
Location ~* \.(gif|jpg|jpeg)$ {
#匹配以.gif、.jpg、.jpeg結(jié)尾的任何請求,但是,/images/內(nèi)的請求將使用configuration c的配置
[configuratoin D]
}
請求處理匹配結(jié)果示例:
/ -> configuration A;
/documents/document.html -> configuration B;
/images/1.gif -> configuration c;
/documents/1.jpg -> configuration D;
例1:域名跳轉(zhuǎn)
輸入www.sina.com,跳轉(zhuǎn)到www.sohu.com
server {
listen 80;
server_name www.sina.com;
access_log logs/sina.access.log main;
location / {
root /web/sina;
index index.html index.htm;
if (-e $request_filename){
# -e 是否存在
rewrite ^/ http://www.sohu.com/ permanent;
# ^/ 域名以/開頭。//www.sina.com ,也可以寫為.* 任意都跳轉(zhuǎn)
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.sohu.com;
access_log logs/sohu.access.log main;
location / {
root /web/sohu;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
例2:靜態(tài)index.html跳轉(zhuǎn)到動態(tài)index.php文件
cd /web/sina/
vi index.php
<?
print_r ($_GET);
?>
Vi nginx.conf
server {
listen 80;
server_name www.sina.com;
access_log logs/sina.access.log main;
location / {
root /web/sina;
index index.html index.htm;
rewrite ^/index(\d+).html /index.php?id=$1 last;
# ^/ 以/開頭。\d+ 多個數(shù)字。()第一個變量。 /index.php?id=$1 把第一個變量賦予id變量,傳入index.php文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /web/sina;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
在客戶端輸入:http://www.sina.com/index2.html
會跳轉(zhuǎn)到index.php,把2當(dāng)變量傳入index.php程序
upstream myweb1 {
server 192.168.190.190:80;
server 192.168.190.191:80;
}
server {
listen 80;
server_name www.sohu.com;
location / {
proxy_pass http://myweb1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server 192.168.244.10:80;
server 192.168.244.11:80;
}
server {
listen 80;
server_name www.sohu.com;
192.168.190.191
#使用www.sohu.com訪問
location / {
proxy_pass http://myweb1;
#使用地址池
proxy_set_header Host $host;
#利用HOST變量向后端服務(wù)器傳遞需要解析的客戶端訪問的域名(傳遞域名)
proxy_set_header X-Forwarded-For $remote_addr;
#$remote_addr 把客戶端真實IP賦予X-Forwarded-For。后端服務(wù)器才能獲取真實的客戶端IP。以便記錄日志,要不日志中記錄的訪問信息都是負(fù)載服務(wù)器,而不是客戶端(傳遞IP)
}
}
26.模塊設(shè)置
Error_log
#錯誤日志
Include
#包含子配置文件,0.6版本以后子配置文件放在nginx.conf所在的路徑下
Pid
#主進(jìn)程id號
User
#nginx nginx表明nginx進(jìn)程的執(zhí)行者和組
Worker_processes
#與cpu個數(shù)相同,4核cpu為4
Worker_rlimit_nofile 65535
#打開的文件描述符,不過提前得設(shè)置ulimit -SHn 65535,即linux允許的打開文件個數(shù)
Worker_connectiones 65535
#客戶端最大連接數(shù)65535
Alias
#虛擬目錄
Error_page
#404,500錯誤跳轉(zhuǎn)頁面
Index
#index index.html,設(shè)置默認(rèn)首頁
Keepalive_timeout
#即tcp持續(xù)連接超時時間
Limit_rate
#limit _rate 100k,即限速為100KB/s
Limit_rate_after
#limit_rate_after 1m,即下載文件超過1m,則進(jìn)入limit_rate限速階段
Listen
#listen 192.168.100.1:80,即設(shè)置ip和端口
Location
#該指令允許對不同的uri進(jìn)行不同的配置,可以是字符串、正則表達(dá)式
Resolver
#resolver 8.8.8.8,為nginx設(shè)置dns域名指向
Root
#設(shè)置網(wǎng)站根目錄
Send_timeout
#超時時間是指進(jìn)行了兩次tcp握手,還沒有轉(zhuǎn)為established狀態(tài)的時間,如果超過這個時間,客戶沒有響應(yīng),nginx則關(guān)閉連接,可以用來防止ddos攻擊
Sendfile
#啟用或禁用sendfile()函數(shù),作用于拷貝兩個文件描述符之間的操作函數(shù),這個拷貝是在內(nèi)核中操作的,比read和write拷貝高效得多
Server
#普通web配置或虛擬主機(jī)的配置的區(qū)域
Server_name
#根據(jù)客戶端請求header頭信息中的host域名,來匹配該請求應(yīng)該由哪個虛擬主機(jī)配置或服務(wù)器的ip
Tcp_nodelay
#封裝tcp/ip數(shù)據(jù)包的等待時間,也叫納格算法,在keepalive開啟才有用
Tcp_nopush
#要求sendfile開啟的時候才起作用,設(shè)置該選擇的原因是nginx在linux上,試圖在一個包中發(fā)送它的httpd應(yīng)答頭
Allow
#allow 192.168.100.254,只允許192.168.100.254訪問
Deny
#deny all,拒絕其他任何人訪問
Autoindex
#autoindex on,即開啟列表頁功能
Charset
#charset utf8;source_charset gbk,把服務(wù)器上的gbk網(wǎng)頁編碼轉(zhuǎn)換成utf8輸出給客戶端
Fastcgi_pass
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#include fastcgi_params;
#fastcgi_pass后跟的是php-cgi進(jìn)程的ip和端口
Access_log
#正確日志
Proxy_pass
# proxy_pass http://myweb1,即后跟的是nginx負(fù)載池upstream中的服務(wù)器
Proxy_set_header
# proxy_set_header Host $host,設(shè)置把$host帶給后端的nginx服務(wù)器
Proxy_temp_path
#用戶指定一個本地目錄緩沖較大的請求,類似于client_body_temp_path
Stub_status
# stub_status on,即開戶狀態(tài)監(jiān)控
Image_filter
#它指定適用于圖片的轉(zhuǎn)換類型
相關(guān)文章
在 Ubuntu 中使用 NTP 進(jìn)行時間同步設(shè)置
本篇文章主要介紹了在 Ubuntu 中使用 NTP 進(jìn)行時間同步設(shè)置,通常客戶端向服務(wù)器請求當(dāng)前的時間,并根據(jù)結(jié)果來設(shè)置其時鐘。2017-04-04Linux 啟動停止SpringBoot jar 程序部署Shell 腳本的方法
這篇文章主要介紹了Linux 啟動停止SpringBoot jar 程序部署Shell 腳本的方法,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04淺析Linux中使用nohup及screen運(yùn)行后臺任務(wù)的示例和區(qū)別
這篇文章主要介紹了Linux中使用nohup及screen運(yùn)行后臺任務(wù)的示例和區(qū)別,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07eclipse3.2.2 + MyEclipse5.5 + Tomcat5.5.27 配置數(shù)據(jù)庫連接池
首先有一點(diǎn)要明確, 我是個新手, 而我所知道的只是一個表面, 根源我并不了解, 目前也不打算去深究. 所以我的方法, 可能只適用于eclipse3.2.2 + MyEclipse5.5 + Tomcat5.5.27 的數(shù)據(jù)庫連接池的配置, 其他版本我無法保證.至少Tomcat 6的admin包還沒出, 所以我的方法肯定不適用于Tomcat 6.2008-10-10