Nginx安裝及配置

Nginx (engine x) 是一個高性能的HTTP和反向代理web服務(wù)器,同時也提供了IMAP/POP3/SMTP服務(wù)。Nginx是由伊戈爾·賽索耶夫為俄羅斯訪問量第二的Rambler.ru站點(俄文:Рамблер)開發(fā)的,第一個公開版本0.1.0發(fā)布于2004年10月4日。
其將源代碼以類BSD許可證的形式發(fā)布,因它的穩(wěn)定性、豐富的功能集、示例配置文件和低系統(tǒng)資源的消耗而聞名。2011年6月1日,nginx 1.0.4發(fā)布。
Nginx是一款輕量級的Web 服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器,在BSD-like 協(xié)議下發(fā)行。其特點是占有內(nèi)存少,并發(fā)能力強,事實上nginx的并發(fā)能力在同類型的網(wǎng)頁服務(wù)器中表現(xiàn)較好,中國大陸使用nginx網(wǎng)站用戶有:百度、京東、新浪、網(wǎng)易、騰訊、淘寶等。

一、nginx可以實現(xiàn)的功能
        1、可以作為靜態(tài)資源的web服務(wù)器、可以緩存文件的資源描述符——加速
        2、支持對http、stmp、pop3等多種協(xié)議的反向代理
        3、支持實現(xiàn)緩存和負載均衡
        4、支持fcgi
        5、支持uWSCGI
        6、支持模塊化
        7、支持過濾器,對特定文件進行壓縮傳輸
        8、支持ssl
        9、支持圖像大小調(diào)整

二.、nginx的特性
        1、模塊
        2、高性能
        3、低內(nèi)存消耗
        4、支持熱部署
        5、支持異步IO
        6、支持事件驅(qū)動
        7、支持內(nèi)存映射

三、核心模塊
        1、標準http模塊
        2、拓展http模塊
        3、郵件拓展模塊
        4、第三方模塊

四、安裝依賴
[root@156 ~]# yum groupinstall "Development Tools" -y
[root@156 ~]# yum install pcre-devel openssl openssl-devel -y 

五、安裝nginx
(1)創(chuàng)建nginx的運行用戶
[root@156 ~]# groupadd -r nginx
[root@156 ~]# useradd -r -s /sbin/nologin -g nginx nginx
[root@156 ~]# tar xvf nginx-1.6.2.tar.gz 
[root@156 ~]# cd nginx-1.6.2
[root@156 nginx-1.6.2]# ./configure --help | more
[root@156 nginx-1.6.2]# ./configure \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--user=nginx --group=nginx \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_ssl_module \
--with-http_stub_status_module \
--without-http_gzip_module \
--with-http_mp4_module \
--with-http_flv_module \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
[root@156 nginx-1.6.2]# make && make install

(2)創(chuàng)建臨時文件的保存目錄
[root@156 nginx-1.6.2]# mkdir /var/tmp/nginx/{client,proxy,fcgi} -pv
mkdir: created directory `/var/tmp/nginxclient'
mkdir: created directory `/var/tmp/nginxproxy'
mkdir: created directory `/var/tmp/nginxfcgi'

(3)啟動nginx
[root@156 ~]# /usr/local/nginx/sbin/nginx
[root@156 ~]# ss -tnl | grep 80
LISTEN     0      128                       *:80   
[root@156 ~]# lsof -i :80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   8395  root    6u  IPv4  20287      0t0  TCP *:http (LISTEN)
nginx   8396 nginx    6u  IPv4  20287      0t0  TCP *:http (LISTEN)
[root@156 ~]# ps aux | grep nginx
root      8395  0.0  0.1  44572  1040 ?        Ss   22:05   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     8396  0.0  0.1  45000  1620 ?        S    22:05   0:00 nginx: worker process      
root      8513  0.0  0.0 103308   848 pts/0    S+   22:39   0:00 grep nginx
測試:在瀏覽器上輸入 服務(wù)器的IP地址即可訪問nginx界面,例如 http://192.168.70.156/

nginx配置詳解
六、和nginx正常運行相關(guān)的配置
    1、worker_processes  1;
         設(shè)置發(fā)起幾個nginx的worker進程

    2、user  userName [groupName];
          指定nginx以誰的身份來運行

    3、pid /path/to/file
         pid文件

    4、worker_rlimit_nofile number;
         設(shè)置所有worker進程一共可以打開的文件的最大數(shù)量值
       (linux上默認每個用戶可以打開1024個文件、套接字也是一個文件)

七、nginx性能優(yōu)化相關(guān)配置
    5、worker_processes  number | auto;
         設(shè)置發(fā)起幾個nginx的worker進程
         number:應(yīng)該比物理核心少一個
         auto:讓系統(tǒng)自動判斷

    6、worker_cpu_affinity cpumask ……| auto;
         設(shè)置cpu掩碼,將worker進程綁定在一個固定的cpu
         舉例:
         worker_processes   4;
         worker_cpu_affinity    0001 0010 0100 1000;    
         舉例:
         worker_processes   2;
         worker_cpu_affinity    0001 0100;
         注意:可以用auto,但是是1.9.10之后可以用

   7、time_resolution interval;
         降低發(fā)起系統(tǒng)調(diào)用gettimeofday()次數(shù) 
         time_resolution 100ms;

   8、worker_priority number;
        修改worker進程的nice值(默認是0)

八、和事件相關(guān)的配置
    9、accept_mutex on | off
          設(shè)置master進程將客戶端的請求調(diào)度到worker進程的
          調(diào)度方式:輪詢、隨機 
          on:使用輪詢
          默認方式:off

   10、accept_mutex_delay time; 默認500ms
          設(shè)置master延遲多久才將客戶端的請求調(diào)度到worker進程

   11、user [epoll|poll|select] 
          通常是不需要指定連接的處理方法(起始就是所使用的事件模型)的 
         建議讓系統(tǒng)自動判斷所要使用的方法

   12、worker_connections number;
          指定每個worker進程可以處理的最大并發(fā)連接的數(shù)量
          默認是1024

   13、master_process on | off;
          指定是否使用master-worker
          默認是:on

九、和調(diào)試和定位bug相關(guān)配置
   14、damon off | on
           指定nginx工作在前臺還是后臺
        默認是 on 

   15、error_log file [level];
           指定錯誤日志的保存位置以及日志級別
            總結(jié):經(jīng)常要修改的參數(shù)
            worker_processes
            worker_connections
            worker_cpu_affinity
            worker_priority

十、和http相關(guān)的配置
[root@156 ~]# vim /etc/profile.d/nginx.sh
export PATH=$PATH:/usr/local/nginx/sbin
[root@156 ~]# source /etc/profile.d/nginx.sh    
[root@156 ~]# nginx
[root@156 ~]# nginx -s reload
[root@156 ~]# ss -tnl | grep 80 
上下文:http {指令}
例子:http{
    全局配置:對所有的虛擬主機都生效的配置
    sendfile on;
    keepalive_timeout 10;
    ……
    虛擬主機配置
    server{
        listen 80;指定該虛擬主機所監(jiān)聽的端口
        server_name www.lichao.com;指定虛擬主機的域名
        root /vhost/web1;指定虛擬主機的網(wǎng)站根目錄
        location /{

        }
    }
    server{

    }
}           

    16、server_name
            設(shè)定虛擬主機的域名,可以使用正則表達式來表示域名
            適用范圍:server上下文中

            server{
                server_name www.zxhk.com;

            }
            server {
                server_name *.zxhk.com;

            }
            server {
                server_name www.zxhk.*;

            }
            server {
                server_name *;

            }
            server {
                server_name ~ ^.*\.www.zxhk.com;

            }

            www.zxhk.com

注意:nginx在進行域名匹配的時候,是有一定的規(guī)則,匹配次序如下
    1、做精確匹配,server_name中的內(nèi)容必須和用戶請求的內(nèi)容完全一樣
    2、檢查左側(cè)的通配符
    3、檢查右側(cè)的通配符
    4、檢查通配符
    5、檢查正則表達式
    6、通過所有檢查都失敗,則訪問default_server,如果沒有指定default_server,
    那么第一個server就是default_server

    17、listen
        指定所監(jiān)聽的套接字
        例子:
        listen 127.0.0.1:80;
        listen 172.0.0.1;
        listen 80;
        listen *:80;
        listen localhost:80;

    18、root 
        指定網(wǎng)站根目錄
        使用范圍:http、server、location
        注意:如果將root寫在了http部分,則會對全部的server都有效

    例子:
        [root@156 ~]# cd /etc/nginx/
        [root@156 nginx]#cp nginx.conf{,.bak}
        [root@156 nginx]#vim nginx.conf
        [root@156 ~]# mkdir -pv /vhost/web1
        [root@156 ~]# echo "<h2>test for web1</h2>">/vhost/web1/index.html  
        [root@156 ~]# cat /vhost/web1/index.html
        [root@156 ~]# nginx -s reload
        http://192.168.70.156/
        test for web1

19、location
        location [ = | ~ | ~* | ^~ ] uri {……}
        url:統(tǒng)一資源定位符
        uri:統(tǒng)一資源標識符
        server{
            server_name www.zxhk.com;
            listen 80;
            location / {
                root /vhost/web1;
            }
        }
        這里的location的含義就是將用戶所訪問的根映射為/vhost/web1路徑
        有多個location的例子
        server{
            server_name www.zxhk.com;
            listen 80;

            location / {
                root /vhost/web1;
            }

            location /img/ {
                root /tmp/img;
            }

            location ~ \.php$ {
                root /phpfile;
            }
            }

        location作路徑映射的時候,有多種方式
        不帶符號 
        =
        ~
        ~*
        ^~

        1、不帶符號,如下
            location /img {
                root /vhost/web1;
            }

            http://www.baidu.com/img
            http://www.baidu.com/a.html

        2、= 表示的是精確匹配
            location = /img {
                root /vhost/web1;
            } 

            要求只有用戶所請求的uri和location所指定的uri必須
            完全一致,否則匹配失敗,如下
            http://www.baidu.com/img               <<匹配成功
            http://www.baidu.com/img/index.html    <<匹配失敗

        3、~表示對用戶所請求的uri做正則表達式,檢查過程中區(qū)分大小寫
             location ~ .jgp$ {
                    root /vhost/web1;
            }
        4、~*表示對用戶所請求的uri做正則表達式,檢查過程中不區(qū)分大小寫
        5、^~不對用戶所請求的uri做正則表達式檢查,而是只檢查uri的左半部分

        各種寫法的優(yōu)先級
        精確匹配優(yōu)先級最高(=)
        uri的左半部檢查
        區(qū)分大小寫的正則表達式檢查
        不區(qū)分大小寫的正則表達式檢查
        不帶符號的location

        location /img {
            root /vhost/web1;
        }

    [root@156 ~]# cd /vhost/web1/
    [root@156 web1]# mkdir img
    [root@156 web1]# echo "this is test" >index.html 
    [root@156 web1]# echo "this is image" >img/index.html 
    http://192.168.70.156/
    this is test
    http://192.168.70.156/img/index.html
    this is image

    [root@156 ~]# vim /etc/nginx/nginx.conf
            server {
                listen 80;
                server_name www.zxhk.com;
                location / {
                    root /vhost/web1/;
                }

                location /img/ {
                    root /vhost/web2/;
                }

        }
    [root@156 ~]# cd /vhost/
    [root@156 vhost]# mkdir web2
    [root@156 vhost]# echo "this is web2" > web2/index.html
    [root@156 vhost]# source /etc/profile.d/nginx.sh 
    [root@156 vhost]# nginx -s reload
        http://192.168.70.156/img    
        404 Not Found 
    [root@156 vhost]# vim /var/log/nginx/error.log 
        2019/09/05 08:52:38 [error] 1915#0: *1 open() "/vhost/web1/favicon.ico" failed
        (2: No such file or directory),
    [root@156 vhost]# cd web2/
    [root@156 web2]# ls
    index.html
    [root@156 web2]# mkdir img
    [root@156 web2]# mv index.html  img/
    http://192.168.70.156/img 
    this is web2 

    20、alias path
           將用戶所請求的uri映射為一個指定的路徑 

server {
                                  listen 80;
                                    server_name www.zxhk.com;
                                    location /img1/ {
                                                    root /vhost/web1/;
                                    }

                                    location /img2/ {
                                                    alias /vhost/web1/;
                                    }
                }
cd /vhost/web1
ls
echo "/vhost/web1">index.html
mkdir img1
echo "/vhost/web1/img1">img1/index.html
source /etc/profile.d/nginx.sh 
nginx -t
nginx -s reload
ls
mkdir img2
echo "/vhost/web2/img/2">img2/index.html
tree
http://192.168.70.156/img2/
/vhost/web1
http://192.168.70.156/img2
404 Not Found

vim /var/log/nginx/error.log 
2019/09/05 09:51:08 [error] 2392#0: *13 open() 
"/usr/local/nginx/html/img2" failed (2: No such 
file or directory), client: 192.168.70.1, server: 
www.zxhk.com, request: "GET /img2 HTTP/1.1", host:
"192.168.70.156"

分享標題:Nginx安裝及配置
文章鏈接:http://bm7419.com/article34/ipoppe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、電子商務(wù)、網(wǎng)站改版、云服務(wù)器、企業(yè)建站、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名