nginx的概述及常見Web服務器的介紹:
Unix和Linux平臺下的常用Web服務器有Apache、 Nginx、 Lighttpd、 Tomcat、 IBM WebSphere等。其中目前應用最廣泛的Web服務器是Apache。 Windows平臺下最常用的服務器則是微軟公司的IIS(Internet Information Server)。
1)Apache服務器
Apache 起初由 Illinois 大學 Urbana-Champaign 的國家高級計算程序中心開發。此後Apache 被開放源代碼團體的成員不斷的發展和加強。1996年4月以來,Apache一直是Internet上最流行的HTTP服務器,1999年5月它在 57% 的網頁服務器上運行,到了2005年7月這個比例上升到69%。Apache是目前世界上用的最多的Web服務器,它有優勢主要在於源代碼開放、有一支開放的開發隊伍、支持跨平臺的應用(可以運行在幾乎所有的Unix、Linux、Windows系統平臺之上)。Apache的模塊支持非常豐富,以至於它提供了非常完善的功能。
Apache的官方網站: http://www.apache.org
2)Lighttpd 服務器
Lighttpd是一個德國人領導的開源軟件,其根本的目的是提供一個專門針對高性能網站,安全、快速、兼容性好並且靈活的Web server環境。具有非常低的內存開銷,cpu佔用率低,效能好,以及豐富的模塊等特點。Lighttpd是衆多OpenSource輕量級的Web server中較爲優秀的一個。支持FastCGI, CGI, Auth, 輸出壓縮(output compress), URL重寫, Alias等重要功能,而Apache之所以流行,很大程度也是因爲功能豐富,在Lighttpd 上很多功能都有相應的實現了,這點對於Apache的用戶是非常重要的,因爲遷移到Lighttpd就必須面對這些問題。
Lighttpd的官方網站: http://www.lighttpd.net
3)Tomcat服務器
Tomcat是一個免費的開源的Serlvet容器,它是Apache基金會的Jakarta項目中的一個核心項目,由Apache、Sun和其它一些公司及個人共同開發而成。由於有了Sun的參與和支持,最新的Servlet和Jsp規範總能在Tomcat中得到體現。Tomcat即是一個Jsp和Servlet的運行平臺。同時Tomcat又不僅僅是一個Servlet容器,它也具有傳統的Web服務器的功能:處理Html頁面,但是與Apache相比,它的處理靜態Html的能力就不如Apache,我們可以將Tomcat和Apache集成到一塊,讓Apache處理靜態Html,而Tomcat處理Jsp和Servlet。這種集成只需要修改一下Apache和Tomcat的配置文件即可。
Tomcat的官方網站: http://tomcat.apache.org
4)IBM WebSphere (weblogic/jboss)
WebSphere Application Server 是 一 種功能完善、開放的Web應用程序服務器,是IBM電子商務計劃的核心部分。基於Java和Servlets的Web應用程序運行環境,包含了爲Web站點提供服務所需的一切,運 行 時 可 以 協 同 並 擴 展Apache、Netscape、 IIS 和IBM 的HTTPWeb服務器,因此可以成爲強大的Web應用服務器。
WebSphere的官方網站: http://www.ibm.com
5)Windows IIS
IIS 是Internet Information Server的縮寫,它是微軟公司主推的服務器。IIS與Window NT Server完全集成在一起,因而用戶能夠利用Windows NT Server和NTFS(NT File System,NT的文件系統)內置的安全特性,建立強大,靈活而安全的Internet和Intranet站點。 IIS支持HTTP(Hypertext Transfer Protocol,超文本傳輸協議),FTP(File Transfer Protocol,文件傳輸協議)以及SMTP協議,通過使用CGI和ISAPI,IIS可以得到高度的擴展。
IIS的官方網站: http://www.iis.net
6)Nginx的介紹
Nginx是俄羅斯人編寫的十分輕量級的HTTP服務器,Nginx,它的發音爲“engine X”, 是一個高性能的HTTP和反向代理服務器,同時也是一個IMAP/POP3/SMTP/FTP 代理服務器。Nginx是由俄羅斯人 Igor Sysoev爲俄羅斯訪問量第二的 Rambler.ru站點開發的,它已經在該站點運行超過兩年半了。Igor Sysoev在建立的項目時,使用基於BSD許可。自Nginx 發佈四年來,Nginx 已經因爲它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。
在俄羅斯許多大網站都已經使用它, 且一直表現不凡。截至2007年4月,俄羅斯大約有20%左右的虛擬主機是由nignx服務或代理的。Google在線安全博客中統計Nginx服務或代理了大約所有Internet虛擬主機的4%。而Netcraft的統計顯示,Nginx服務的主機在過去的一年裏以四倍的速度增長並且在這幾年裏,它的排名還在不斷上升,下圖爲Netcraft截止至2012年1月的統計。 (http://news.netcraft.com/)
nginx安裝和啓動
安裝pcre
安裝openssl
安裝zlib
安裝nginx
yum -y install gcc openssl-devel pcre-devel zlib-devel
yum -y install nginx
瞭解軟件目錄結構
[root@node1 ~]# find /opt/nginx/ -maxdepth 1 -type d
/opt/nginx/
/opt/nginx/conf 配置文件
/opt/nginx/logs 日誌
/opt/nginx/html 頁面家目錄
/opt/nginx/sbin 二進制命令
[root@node1 ~]# find /opt/nginx/
/opt/nginx/
/opt/nginx/conf
/opt/nginx/conf/win-utf
/opt/nginx/conf/uwsgi_params.default --模板
/opt/nginx/conf/fastcgi_params.default
/opt/nginx/conf/mime.types ---識別類型
/opt/nginx/conf/nginx.conf ---主配置文件
/opt/nginx/conf/uwsgi_params --內部變量
/opt/nginx/conf/scgi_params.default
/opt/nginx/conf/fastcgi_params --PHP相關
/opt/nginx/conf/scgi_params --程序
/opt/nginx/conf/koi-utf ---編碼
/opt/nginx/conf/fastcgi.conf ---fastcgi配置文件
/opt/nginx/conf/mime.types.default
/opt/nginx/conf/fastcgi.conf.default
/opt/nginx/conf/koi-win
/opt/nginx/conf/nginx.conf.default
/opt/nginx/logs
/opt/nginx/html
/opt/nginx/html/index.html ---首頁
/opt/nginx/html/50x.html ---錯誤代碼
/opt/nginx/sbin
/opt/nginx/sbin/nginx --二進制命令
nginx啓動
注意 關閉佔用80端口的程序
[root@node1 ~]# /opt/nginx/sbin/nginx
[root@node1 ~]# netstat -tnlp |grep nginx
[root@node1 ~]# ps aux |grep nginx
平滑方式關閉(正常退出服務)
[root@node1 ~]# pkill -15 nginx
直接關閉
[root@node1 ~]# pkill -9 nginx
完善環境變量
[root@node1 ~]# echo "export PATH=$PATH:/opt/nginx/sbin" >> ~/.bashrc
[root@node1 ~]# which nginx
/opt/nginx/sbin/nginx
定製啓動腳本
[root@node1 ~]# cp /root/lnmp/nginx.init.d /etc/rc.d/init.d/nginx
[root@node1 ~]# chmod +x /etc/rc.d/init.d/nginx
[root@node1 ~]# chkconfig nginx on
[root@node1 ~]# service nginx restart
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
檢測配置文件的語法
[root@node1 ~]# nginx -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
[root@node1 ~]# cat /opt/nginx/conf/nginx.conf |grep -v '^$' |grep -v '.*#'
user nginx nginx;
worker_processes 2;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$<a href="http_referer" "="">http_referer" '
'"$<a href="http_user_agent" "="">http_user_agent" "$<a href="http_x_forwarded_for" ';"="">http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name www.abc.com;
charset utf-8;
access_log logs/www.abc.access.log main;
location / {
root /opt/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@node1 ~]# ulimit -HSn 65535
[root@node1 ~]# service nginx restart
echo "192.168.0.1 <a href="http://www.abc.com" "="">www.abc.com" >> /etc/hosts
elinks http://www.abc.com
Nginx 網站服務配置示例
添加 nginx 狀態統計
Nginx下的虛擬主機配置
訪問控制
+++nginx 正則表達式+++
location的匹配順序:
1、= 嚴格匹配這個查詢,如果匹配到,則停止搜索
2、~ 匹配正則表達式,區分大小寫
3、~* 匹配正則表達式,不區分大小寫
4、!~* 取反
使用正則表達式,區分大小寫
[root@node1 ~]# vim /opt/nginx/conf/nginx.conf
location ~ ^/a\.html$ {
root /test1;
index index.html;
}
[root@node1 ~]# mkdir /test1
[root@node1 ~]# echo 'this is test1 dir Test page' > /test1/a.html
[root@i ~]# elinks -dump http://192.168.0.1/a.html
this is test1 dir Test page
使用正則表達式,不區分大小寫
location ~* ^/Status {
stub_status on;
}
[root@i ~]# elinks -dump http://192.168.0.1/status
[root@i ~]# elinks -dump http://192.168.0.1/STATUS
精確匹配
[root@node1 ~]# vim /opt/nginx/conf/nginx.conf
location = /a.html {
root /test2;
index index.html;
}
[root@node1 ~]# mkdir /test2
[root@node1 ~]# echo 'this is test2 dir Test page' > /test2/a.html
結論
等號(=)與( / )的對比,結論:等號的優先級別比( / )的高,(其實等號的優先級別是最高的)
正則表達式的優先級別僅次於等號
如果正則表達式之間有衝突,則按照最詳細的正則表達式優先匹配
+++Nginx下的虛擬主機配置+++
基於端口
基於IP
基於域名
-----------------基於端口---------------------
server {
listen 80;
#server_name www.abc.com;
charset utf-8;
access_log logs/www.abc.access.log main;
location / {
root /www1;
index index.html index.htm;
}
location ~* ^/Status {
stub_status on;
}
}
server {
listen 8000;
#server_name www.abc.com;
charset utf-8;
access_log logs/www.abc.access.log main;
location / {
root /www2;
index index.html index.htm;
}
location ~* ^/Status {
stub_status on;
}
}
}
[root@node1 html]# mkdir /www{1..2} -p
[root@node1 html]# echo www1 > /www1/index.html
[root@node1 html]# echo www2 > /www2/index.html
[root@node1 html]# service nginx restart
[root@node1 html]# netstat -tnlp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7201/nginx
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 7201/nginx
[root@i ~]# elinks -dump http://192.168.0.1:80
[root@i ~]# elinks -dump http://192.168.0.1:8000
-----------------基於IP地址---------------------
[root@node1 html]# ifconfig eth0 192.168.0.X1 up
[root@node1 html]# ifconfig eth0:0 192.168.0.X2 up
[root@node1 html]# vim /opt/nginx/conf/nginx.conf
listen 192.168.0.X1:80;
listen 192.168.0.X2:80;
[root@node1 html]# service nginx restart
[root@node1 html]# netstat -tnlp |grep nginx
tcp 0 0 192.168.0.10:80 0.0.0.0:* LISTEN 7283/nginx
tcp 0 0 192.168.0.1:80 0.0.0.0:* LISTEN 7283/nginx
測試:
[root@i ~]# elinks -dump http://192.168.0.1
www1
[root@i ~]# elinks -dump http://192.168.0.10
www2
[root@node1 html]# mkdir /www{1..2} -p
[root@node1 html]# echo www1 > /www1/index.html
[root@node1 html]# echo www2 > /www2/index.html
-------------基於域名------------------------
www.abc.com 192.168.0.1
www.bcd.com 192.168.0.1
---------------
listen 80;
server_name www.abc.com;
listen 80;
server_name www.bcd.com;
--------------
測試過程
[root@i ~]# echo "192.168.0.1 <a href="http://www.abc.com" "="">www.abc.com" >> /etc/hosts
[root@i ~]# echo "192.168.0.1 <a href="http://www.bcd.com" "="">www.bcd.com" >> /etc/hosts
[root@i ~]# elinks -dump http://www.abc.com
www1
[root@i ~]# elinks -dump http://www.bcd.com
www2
訪問控制
目錄的訪問控制:
1、根據賬號密碼進行訪問控制
2、根據客戶端的源地址進行訪問控制
3、結合正則表達式和文件類型做訪問控制
[root@node1 html]# vim /opt/nginx/conf/nginx.conf
location ~* ^/Status {
stub_status on;
auth_basic "status login";
auth_basic_user_file "/opt/nginx/conf/.htpasswd";
}
設置用戶與密碼
[root@node1 html]# vim /opt/nginx/conf/.htpasswd
[root@node1 html]# grub-md5-crypt
Password:
Retype password:
$1$3eyLv$FRqUzfIiHgGJb00B0l0M4/
[root@node1 html]# vim /opt/nginx/conf/.htpasswd
user01:$1$3eyLv$FRqUzfIiHgGJb00B0l0M4/
[root@node1 html]# service nginx restart
[root@i ~]# elinks http://192.168.0.1/status
++++根據客戶端的源地址進行訪問控制++++
location ~* ^/abc/save\.txt$ {
root /www1;
index index.html;
allow 192.168.0.0/24;
}
[root@node1 html]# mkdir /www1/abc
[root@node1 html]# echo "&*&*(" > /www1/abc/save.txt
[root@i ~]# elinks http://www.abc.com/abc/save.txt
允許某一個網段/拒絕某一個IP地址
location ~* ^/abc/save\.txt$ {
root /www1;
index index.html;
deny 192.168.0.254;
allow 192.168.0.0/24;
}
[root@i ~]# elinks -dump http://www.abc.com/abc/save.txt
403 Forbidden
結合正則表達式和文件類型做訪問控制
server {
listen 80;
server_name www.bcd.com;
charset utf-8;
access_log logs/www.abc.access.log main;
location ~* \.(txt|doc) {
root /www2;
index index.html;
deny all;
}
}
[root@node1 www2]# echo deny_test1 > /www2/a1.txt
[root@node1 www2]# echo deny_test2 > /www2/a1.doc
[root@i ~]# elinks -dump http://www.bcd.com/a1.txt
[root@i ~]# elinks -dump http://www.bcd.com/a2.doc
拒絕訪問某一個目錄
location ~* ^/install {
root /www2;
index index.html;
deny all;
}
[root@node1 www2]# mkdir install
[root@node1 www2]# cp /etc/passwd install/
[root@i ~]# elinks -dump http://www.bcd.com/install/passwd
403 Forbidden
禁止單個目錄
location = /searchword/cron/ {
deny all;
}
配置Nginx實現https
443 --- https
httpd ssl(openssl)
證書的頒發機構 CA
/ \
私鑰 證書(公鑰)
# cd /etc/pki/tls/certs/
# make server.key --生成私鑰
# openssl rsa -in server.key -out server.key --去除私鑰的加密密碼
# make server.csr --生成證書頒發機構(CA)
# openssl x509 -in server.csr -req -signkey server.key -days 365 -out server.crt --生成證書
1 CA機構 獲取私鑰與公鑰<證書>
2 發佈頁面的時候填寫私鑰與公鑰
3 對外發布https:443
[root@node1 certs]# vim /opt/nginx/conf/nginx.conf
server {
listen 443;
server_name www.cde.com;
ssl on;
ssl_certificate /etc/pki/tls/certs/server.crt;
ssl_certificate_key /etc/pki/tls/certs/server.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /www3;
index index.html index.htm;
}
}
[root@node1 certs]# service nginx restart
[root@node1 certs]# mkdir /www3
[root@node1 certs]# echo www3 > /www3/index.html
測試
[root@i ~]# echo "192.168.0.1 <a href="http://www.cde.com" "="">www.cde.com" >> /etc/hosts
https://www.cde.com/
整合LNMP
++++++++++++++++++編譯php++++++++++++++++++++
[root@node1 php-5.5.3]# yum -y install libxml2-devel
[root@node1 ~]# tar xf /root/lnmp/php-5.5.3.tar.bz2 -C /usr/local/src/
[root@node1 ~]# cd /usr/local/src/php-5.5.3/
[root@node1 php-5.5.3]# ./configure --prefix=/opt/php5 --with-config-file-path=/opt/php5 --enable-fpm --with-mysql
--prefix=/opt/php5
--with-config-file-path=/opt/php5
--enable-fpm (--enable-fastcgi 舊版本的選項 新的版本選項 --enable-fpm)
--with-mysql
[root@node1 php-5.5.3]# make && make install
配置PHP的 fpm方式
1) 生成php的主配置文件
[root@node1 php-5.5.3]# cp php.ini-production /opt/php5/php.ini
fpm默認配置文件與二進制命令php-fpm
[root@node1 ~]# find /opt/php5/|grep fpm |tail -2
/opt/php5/etc/php-fpm.conf.default
/opt/php5/sbin/php-fpm
2) 生成php-fpm的配置文件
[root@node1 ~]# cd /opt/php5/etc/
[root@node1 etc]# cp php-fpm.conf.default php-fpm.conf
3)啓動與關閉
啓動
[root@node1 etc]# /opt/php5/sbin/php-fpm -c /opt/php5/etc/php-fpm.conf
[root@node1 etc]# netstat -tnlp |grep php
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 19345/php-fpm
[root@node1 etc]# ps aux |grep php
關閉
[root@node1 etc]# pkill -9 php
4) 設置php-fpm
[root@node1 etc]# vim /opt/php5/etc/php-fpm.conf
[www]
user = nobody
group = nobody
listen = 192.168.0.1:9000
pm = dynamic ---動態
pm.max_children = 10 最大開啓10個
pm.start_servers = 4 默認啓動開啓4個
pm.min_spare_servers = 2 空閒最小開啓1個
pm.max_spare_servers = 8 空閒最大開啓2個
[root@node1 etc]# echo "/opt/php5/sbin/php-fpm -c /opt/php5/etc/php-fpm.conf" >> /etc/rc.local
[root@node1 etc]# source /etc/rc.local
[root@node1 etc]# ps aux |grep php |grep pool |wc -l --四個進程
[root@node1 etc]# netstat -tnlp |grep php
tcp 0 0 192.168.0.1:9000 0.0.0.0:* LISTEN 19438/php-fpm
整合lamp
上傳論壇 BBS discuz
[root@node1 ~]# mkdir /webserver/bbs01
[root@node1 lnmp]# unzip Discuz_X3.1_SC_UTF8.zip
[root@node1 lnmp]# mv upload/* /webserver/bbs01/
發佈頁面
[root@node1 lnmp]# vim /opt/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$<a href="http_referer" "="">http_referer" '
'"$<a href="http_user_agent" "="">http_user_agent" "$<a href="http_x_forwarded_for" ';"="">http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name bbs01.uplooking.com;
charset utf-8;
access_log logs/bbs01.uplooking.com.access.log main;
location / {
root /webserver/bbs01;
index index.html index.htm index.php;
}
location ~ \.(php|phtml)$ {
root /webserver/bbs01;
fastcgi_pass 192.168.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#http://bbs01.uplooking.com = /webserver/bbs01/index.php
#$document_root=/webserver/bbs01
#$fastcgi_script_name=index.php /admin.php
}
}
[root@node1 lnmp]# service nginx restart
安裝頁面
[root@node1 lnmp]# rpm -q mysql-server
[root@node1 lnmp]# service mysqld start
[root@node1 lnmp]# chkconfig mysqld on
mysql> create database bbs01 default charset utf8;
mysql> grant all on bbs01.* to 'runbbs01'@'localhost' identified by '123';
[root@node1 lnmp]# cd /webserver/bbs01/
[root@node1 bbs01]# chmod 777 data/ uc_client/ uc_server/ config/ -R
http://bbs01.uplooking.com/install