nginx的安裝配置詳解

nginx的安裝配置詳解

  1. 配置簡單,靈活,輕便
  2. 高併發(靜態小文件),靜態幾萬併發
  3. 佔資源小,2W併發,開10個線程服務,內存只消耗幾百M
  4. 功能種類比較多(web,cache,proxy),可是每一個功能都不是特別強
  5. nginx 能夠配合動態服務(FASTCGI接口)
  6. 利用nginx能夠對ip限速,能夠限制鏈接數
  7. nginx的併發很高,1-2M的小文件,能夠支持同時1-3W的併發

nginx的安裝配置詳解

1. 介紹各個經常使用的服務端口

  1. 21 ftp ;22 ssh;25 smtp;3306 mysql;873 rsync;3389 遠程桌面;161 snmp;111 rpcbind;80 www http;443 https;110 pop3;53 dns;514 rsyslog
  2. 咱們經常使用的nslookup和dig查詢域名解析工具的安裝包爲bind-utils,如yum install bind-utils -y
  3. 咱們一般配好了nginx後,須要測試一下,通常經過curl命令,curl -I www.baidu.com 能夠反饋百度的head信息,能夠直接yum install curl -y進行安裝。
  4. 若是安裝源碼包,有一些編譯工具沒有安裝的話,咱們能夠安裝開發包組Development tools,如yum groupinstall "Development tools" 方式進行安裝。

2. nginx的優點

  1. 配置簡單,靈活,輕便
  2. 高併發(靜態小文件),靜態幾萬併發
  3. 佔資源小,2W併發,開10個線程服務,內存只消耗幾百M
  4. 功能種類比較多(web,cache,proxy),可是每一個功能都不是特別強
  5. nginx 能夠配合動態服務(FASTCGI接口)
  6. 利用nginx能夠對ip限速,能夠限制鏈接數
  7. nginx的併發很高,1-2M的小文件,能夠支持同時1-3W的併發,可是nginx只能處理靜態資源,若是是動態資源就要藉助php和數據庫了,可是後二者併發都比較低
    只有同時300-800的併發,所以nginx若是處理動態資源,主要是看後二者的併發,這取決於木桶原理,極端點取決於短板php和DB。

3. nginx的安裝

  1. 必須先安裝pcre,由於nginx有rewrite功能,rewrite模塊須要pcre的支持,經驗提示,若是安裝什麼提示缺乏什麼庫,通常都是缺devel,只要在前面加個-devel便可
[root@maiyat conf]# rpm -qa pcre
pcre-7.8-7.el6.x86_64
[root@maiyat conf]# rpm -qa pcre-devel
pcre-devel-7.8-7.el6.x86_64
  1. 安裝openssl -devel,由於nginx有安全模塊,所以須要安裝openssl 和openssl-devel
[root@maiyat conf]# rpm -qa openssl
openssl-1.0.1e-57.el6.x86_64
[root@maiyat conf]# rpm -qa openssl-devel
openssl-devel-1.0.1e-57.el6.x86_64
[root@maiyat conf]#
  1. 安裝nginx,先下載源碼包,而後解壓縮,而後經過./configure配置,而後編譯和安裝。其中./configure --help
    能夠查看到一些配置的參數,如不須要的參數用--without-mail_smtp_module取消,./configure只是配置文件,並非安裝,只是生成了一個makefile。
    這裏安裝只有3個組件,其餘必須的都會默認安裝,-user=nginx指定用戶,--group=nginx指定組,--with-http_ssl_module激活sl加密模塊,--with-http_stub_status_module激活狀態信息
wget http://nginx.org/download/nginx-1.6.3.tar.gz
tar xf nginx-1.6.3.tar.gz 
cd nginx-1.6.3
./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
echo $?
make && make install
useradd nginx -s /sbin/nologin -M
id nginx
grep nginx /etc/group
ln -s /application/nginx-1.6.3/ /application/nginx
  1. 注意,有不少人提示沒有安裝gcc,或者make,咱們安裝的時候最好把開發庫工具安裝上去
    yum groupinstall "Development tools" 若是是源碼安裝pcre的話,安裝nginx可能會找不到pcre,而提示沒法找到pcre,
    所以編譯的時候,咱們必須用--with=/usr/local/bin/pcre 指定pcre的位置。
  2. 安裝完成,啓動服務: /application/nginx/sbin/nginx,檢查一下,80端口是否打開,進程是否存在。
[root@maiyat ~]# /application/nginx/sbin/nginx 
[root@maiyat ~]# lsof -i :80                   
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1382  root    6u  IPv4  13412      0t0  TCP *:http (LISTEN)
nginx   1383 nginx    6u  IPv4  13412      0t0  TCP *:http (LISTEN)
[root@maiyat ~]# ps -ef |grep nginx |grep -v grep
root       1382      1  0 23:24 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx      1383   1382  0 23:24 ?        00:00:00 nginx: worker process        
[root@maiyat ~]# 
[root@maiyat ~]# ss -lntup |grep nginx
tcp    LISTEN     0      128                    *:80                    *:*      users:(("nginx",1382,6),("nginx",1383,6))
  1. 在客戶端瀏覽器輸入 192.168.50.2:80,看能不能打開nginx的初使頁面。

4. nginx服務的兩個相關命令

  1. 改完nginx配置文件後,咱們不能着急重啓nginx服務,必須先檢查一下配置文件的語法,如:
[root@maiyat sbin]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  1. 改完nginx配置文件後,咱們必需要重啓nginx才能讓配置生效,重啓的時候要平滑重啓。
[root@maiyat sbin]# /application/nginx/sbin/nginx -s reload
[root@maiyat sbin]#
  1. 查看nginx安裝配置的參數,/application/nginx/sbin/nginx -V,如:
[root@maiyat sbin]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.6.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
TLS SNI support enabled
configure arguments: --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
[root@maiyat sbin]#

5. 安裝目錄文件詳解

  1. nginx安裝完成後文件詳解如:
[root@maiyat nginx]# ls |grep -v temp |xargs tree -L 1
conf 配置文件
|--nginx.conf
html 站點目錄
|-- 50x.html
`-- index.html
logs  日誌信息
|-- access.log
|-- error.log
`-- nginx.pid
sbin  執行文件
`-- nginx
  1. nginx的配置文件nginx.conf,咱們配置的時候能夠先把原來的備份,而後借用nginx.conf.default過來直接配置,配置文件必定要注意{},必須成對出現,不能拆散如:
cat 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;
這幾行屬於main區,Nginx核心功能模塊
 events {
     worker_connections  1024;     一個worker所能處理的多少的併發鏈接數
 14 }
 這幾行屬於event區,Nginx核心功能模塊
http {
     include       mime.types;
     default_type  application/octet-stream;
 這個屬於http區開始,Nginx核心功能模塊
 ##################################################
 實例搭建兩個虛擬主機www.maiyat.com  bbs.maiyat.com
 ##################################################
cp nginx.conf nginx.conf.bak
egrep -v "#|^$" nginx.conf.default > nginx.conf
worker_processes  1;     worker進程的數量
events {                 事件區塊的開始
    worker_connections  1024; 每一個woker進程支持的最大鏈接數
}  事件區塊結束
http {             http區塊開始
    include       mime.types;  Nginx支持的媒體類型庫文件包含
    default_type  application/octet-stream;  默認的媒體類型
    sendfile        on;                  開啓高效傳輸模式
    keepalive_timeout  65;                鏈接超時
    server {                 第一個server區塊開始,表明一個獨立的虛擬主機站點
        listen       80;        提供服務的端口,默認是80
        server_name  www.maiyat.com;  提供服務的域名主機名
        location / {                      第一個Location區塊的開始
            root   html/www;              站點的根目錄,相對路徑,相對於Nginx的安裝路徑
            index  index.html index.htm;   默認的首頁文件,多個用空格分開
        }                                    第一個區塊結束
}
        server {
        listen       80;
        server_name  bbs.maiyat.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
		        error_page   500 502 503 504  /50x.html;         出現對應的http狀態碼使用50x.html迴應
        location = /50x.html {                        Location區塊的開始,訪問50x.html
            root   html;                    指定對應的站點目錄爲html
			}
    }
}                 http區塊結束
  1. 測試www.maiyat.com 及bbs.maiyat.com有沒有搭建好
    3.1 先在/etc/hosts解析好,能夠一個ip後面跟幾個不一樣的域名,如
[root@maiyat conf]# cat /etc/hosts
#127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1       maiyat  www.maiyat.com  bbs.maiyat.com  blog.maiyat.com
192.168.50.2    maiyat  www.maiyat.com  bbs.maiyat.com  blog.maiyat.com

3.2 若是是windows的hosts文件在 C:\Windows\System32\drivers\etc
3.3 linux下用curl命令看能不能返回消息,windows下用瀏覽器輸入域名php

[root@maiyat conf]# curl www.maiyat.com
hello world
[root@maiyat conf]# curl bbs.maiyat.com
hello oldboy
[root@maiyat conf]# curl blog.maiyat.com
happy comeon maiyat.com !

6. 虛擬主機還可使用端口和IP進行配置

  1. 上述講的是最經常使用的經過域名來配置虛擬主機,可是虛擬主機還能夠經過端口和ip進行配置。如基於端口的配置,直接在配置文件的listen 80,80改成8001或者其餘的就行了,其餘的都不用改,注意要把幾個虛擬主機的域名統一,須要重啓nginx服務。
[root@maiyat conf]# vi nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       8001;
        server_name  www.maiyat.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
}
        server {
        listen       8002;
        server_name  www.maiyat.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
server {
        listen       8003;
        server_name  www.maiyat.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}
[root@maiyat conf]# cd ../sbin/
[root@maiyat sbin]# ./nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[root@maiyat sbin]# ./nginx -s reload
[root@maiyat sbin]# lsof -i :8001
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1382  root   10u  IPv4  13964      0t0  TCP *:vcom-tunnel (LISTEN)
nginx   1476 nginx   10u  IPv4  13964      0t0  TCP *:vcom-tunnel (LISTEN)
[root@maiyat sbin]# lsof -i :8002
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1382  root   11u  IPv4  13965      0t0  TCP *:teradataordbms (LISTEN)
nginx   1476 nginx   11u  IPv4  13965      0t0  TCP *:teradataordbms (LISTEN)
[root@maiyat sbin]# lsof -i :8003
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1382  root   12u  IPv4  13966      0t0  TCP *:mcreport (LISTEN)
nginx   1476 nginx   12u  IPv4  13966      0t0  TCP *:mcreport (LISTEN)
[root@maiyat sbin]# curl www.maiyat.com:8001
hello world
[root@maiyat sbin]# curl www.maiyat.com:8002
hello oldboy
[root@maiyat sbin]# curl www.maiyat.com:8003
happy comeon maiyat.com !

2 基於ip進行配置虛擬主機,注意修改的時候把端口恢復爲80。html

[root@maiyat sbin]# cd ../conf
[root@maiyat conf]# vi nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       192.168.50.250:80;
        server_name  www.maiyat.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
}
        server {
        listen       192.168.50.251:80;
        server_name  www.maiyat.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
server {
        listen       192.168.50.253:80;
        server_name  www.maiyat.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}
[root@maiyat conf]# 
[root@maiyat conf]# cd ../sbin/
[root@maiyat sbin]# ./nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: [emerg] bind() to 192.168.50.250:80 failed (99: Cannot assign requested address)
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test failed
[root@maiyat sbin]# ip addr add 192.168.50.250/24 dev eth0 
[root@maiyat sbin]# ip addr add 192.168.50.251/24 dev eth0 
[root@maiyat sbin]# ip addr add 192.168.50.253/24 dev eth0 
[root@maiyat sbin]# ./nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[root@maiyat sbin]# ./nginx -s reload
[root@maiyat sbin]# curl 192.168.50.250
hello world
[root@maiyat sbin]# curl 192.168.50.251
hello oldboy
[root@maiyat sbin]# curl 192.168.50.253
happy comeon maiyat.com
相關文章
相關標籤/搜索