1.什么是Nginx
Nginx是一個(gè)使用c語言開發(fā)的高性能的http服務(wù)器及反向代理服務(wù)器自阱。
Nginx是一款高性能的http 服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器嘉抓。由俄羅斯的程序設(shè)計(jì)師Igor Sysoev所開發(fā)石蔗,官方測(cè)試nginx能夠支支撐5萬并發(fā)鏈接是晨,并且cpu窍霞、內(nèi)存等資源消耗卻非常低犁嗅,運(yùn)行非常穩(wěn)定无虚。
2.能做什么
- http服務(wù)器缔赠。Nginx是一個(gè)http服務(wù)可以獨(dú)立提供http服務(wù)∮烟猓可以做網(wǎng)頁靜態(tài)服務(wù)器嗤堰。
- 虛擬主機(jī)《然拢可以實(shí)現(xiàn)在一臺(tái)服務(wù)器虛擬出多個(gè)網(wǎng)站踢匣。例如個(gè)人網(wǎng)站使用的虛擬主機(jī)。
- 反向代理戈抄,負(fù)載均衡离唬。當(dāng)網(wǎng)站的訪問量達(dá)到一定程度后,單臺(tái)服務(wù)器不能滿足用戶的請(qǐng)求時(shí)划鸽,需要用多臺(tái)服務(wù)器集群可以使用nginx做反向代理输莺。并且多臺(tái)服務(wù)器可以平均分擔(dān)負(fù)載,不會(huì)因?yàn)槟撑_(tái)服務(wù)器負(fù)載高宕機(jī)而某臺(tái)服務(wù)器閑置的情況。
3.如何使用
和大多數(shù)服務(wù)器一樣模闲,需要經(jīng)過安裝和配置兩步建瘫,Nginx一般推薦安裝到linux系統(tǒng),而且要安裝c語言的編譯環(huán)境gcc
- 安裝:
安裝之前的依賴
1.gcc
安裝nginx需要先將官網(wǎng)下載的源碼進(jìn)行編譯尸折,編譯依賴gcc環(huán)境啰脚,如果沒有g(shù)cc環(huán)境,需要安裝gcc:yum install gcc-c++
2.PCRE
PCRE(Perl Compatible Regular Expressions)是一個(gè)Perl庫实夹,包括 perl 兼容的正則表達(dá)式庫橄浓。nginx的http模塊使用pcre來解析正則表達(dá)式,所以需要在linux上安裝pcre庫亮航。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre開發(fā)的一個(gè)二次開發(fā)庫荸实。nginx也需要此庫。
- zlib
zlib庫提供了很多種壓縮和解壓縮的方式缴淋,nginx使用zlib對(duì)http包的內(nèi)容進(jìn)行g(shù)zip准给,所以需要在linux上安裝zlib庫。
yum install -y zlib zlib-devel
4.openssl
OpenSSL 是一個(gè)強(qiáng)大的安全套接字層密碼庫重抖,囊括主要的密碼算法露氮、常用的密鑰和證書封裝管理功能及SSL協(xié)議,并提供豐富的應(yīng)用程序供測(cè)試或其它目的使用钟沛。
nginx不僅支持http協(xié)議畔规,還支持https(即在ssl協(xié)議上傳輸http),所以需要在linux安裝openssl庫恨统。
yum install -y openssl openssl-devel
參考官網(wǎng)給出的說明:http://nginx.org/en/docs/
打開上面的采用編譯安裝方式的文檔叁扫,文檔最后給出了編譯安裝的示例,文檔中也給出了各個(gè)配置的用途http://nginx.org/en/docs/configure.html
4.具體步驟
1.下載linux環(huán)境的nginx安裝包
地址:http://nginx.org/en/download.html
2.將下載的安裝包傳到linux環(huán)境并解壓
3.進(jìn)入解壓后的目錄畜埋,執(zhí)行configure
./configure
--prefix=/usr/local/nginx
--pid-path=/var/run/nginx/nginx.pid
--lock-path=/var/lock/nginx.lock
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--with-http_gzip_static_module
--http-client-body-temp-path=/var/temp/nginx/client
--http-proxy-temp-path=/var/temp/nginx/proxy
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi
--http-scgi-temp-path=/var/temp/nginx/scgi
注意:上邊將臨時(shí)文件目錄指定為/var/temp/nginx莫绣,需要在/var下創(chuàng)建temp及nginx目錄
執(zhí)行完會(huì)多了一個(gè)Makefile
4.執(zhí)行make指令
5.執(zhí)行make install指令
安裝完成
5.常用指令
我們的安裝路徑為/usr/local/nginx ,打開并查看由捎,里面包含conf兔综,html和sbin三個(gè)文件
(html1為本人測(cè)試用,請(qǐng)忽略)
- conf中是nginx的配置文件
- html為資源根目錄
- sbin為執(zhí)行指令目錄
啟動(dòng)nginx服務(wù)指令
進(jìn)入sbin目錄執(zhí)行:./nginx啟動(dòng)
查看進(jìn)程情況
訪問測(cè)試
關(guān)閉nginx服務(wù)
執(zhí)行:./nginx -s quit
6.修改配置文件
進(jìn)入到上面的conf目錄中vim 打開nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
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 "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
配置文件中可以通過配置多個(gè)server來代表多個(gè)虛擬主機(jī)狞玛,包含下面兩種方式
|-指定多個(gè)端口號(hào)來實(shí)現(xiàn)
|-指定多個(gè)域名來實(shí)現(xiàn)
#修改端口方式配置多個(gè)虛擬機(jī)
server {
listen 81;#修改后的端口
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html1;#修改加載資源的根目錄,還記得上面請(qǐng)忽略的的html1嗎涧窒?就是他了
index index.html index.htm;
}
}
#修改域名配置多個(gè)虛擬機(jī)
server {
listen 80;
server_name www.test.com;#這里采用不同的域名對(duì)應(yīng)相同的ip
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html1;
index index.html index.htm;
}
}
7.反向代理和負(fù)載均衡
應(yīng)該有一個(gè)nginx服務(wù)器有多個(gè)應(yīng)用服務(wù)器(可以是tomcat)
可以使用一臺(tái)虛擬機(jī)心肪,安裝一個(gè)nginx,多個(gè)tomcat纠吴,來模擬硬鞍。
反向代理:修改nginx的配置文件
upstream tomcats{
server 192.168.3.222:8080;
server 192.168.3.222:8081;
}
server {
listen 80;
server_name tomcat.taotao.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcats;和上面的tomcats需要對(duì)應(yīng)
index index.html index.htm;
}
}
負(fù)載均衡:修改nginx的配置文件
upstream tomcats{
server 192.168.3.222:8080 weight=2;#通過weight=?來設(shè)置權(quán)重
server 192.168.3.222:8081;
}
server {
listen 80;
server_name tomcat.taotao.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcats;和上面的tomcats需要對(duì)應(yīng)
index index.html index.htm;
}
}