溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Nginx虛擬主機相關配置詳解

發(fā)布時間:2020-06-29 21:02:32 來源:網(wǎng)絡 閱讀:1174 作者:筱振 欄目:系統(tǒng)運維

隨著計算機與Internet技術(shù)的高速發(fā)展,各種各樣的Web站點也就成為面向用戶的中堅力量。在各種網(wǎng)站服務器軟件中。除了Apache外,還有一款輕量級的HTTP服務器軟件——Nginx。

基于Apache搭建Web服務器可以參考博文:基于Apache搭建Web服務器詳解

一、Nginx服務簡介

Nginx由俄羅斯的lgor Sysoev開發(fā),專為性能優(yōu)化而開發(fā),其最知名的優(yōu)點就是它的穩(wěn)定性和低系統(tǒng)資源消耗、以及對HTTP并發(fā)連接的高處立能力(單臺物理服務器可支持30000~50000個并發(fā)請求)。正因為如此,大量提供社交網(wǎng)絡、新聞資訊、電子商務及虛擬主機等服務的企業(yè)紛紛選擇Nginx來提供Web服務。

搭建Web服務如果是為了解析靜態(tài)網(wǎng)頁、動態(tài)網(wǎng)頁等、不需要太多的功能,那么Nginx絕對是首選。

二、編譯安裝Nginx服務

Nginx最新的穩(wěn)定版本為1.12.0,可以通過官網(wǎng)http://nginx.org/ 或者網(wǎng)盤鏈接:https://pan.baidu.com/s/1H5DHcVWMPGDWFQ-kDsS7XA
提取碼:1zyi
進行下載使用。

1.編譯安裝Nginx服務

1)安裝支持軟件

Nginx的配置及運行需要pcre(支持正則表達式)、zlib(支持壓縮)等軟件包的支持,因此應先安裝這些軟件的開發(fā)包,以便提供提供相應的功能,確保Nginx的安裝順利完成:

[root@localhost ~]# yum -y install pcre-devel zlib-devel
2)創(chuàng)建運行用戶、組
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
3)編譯安裝Nginx
[root@localhost ~]# tar zxf nginx-1.12.0.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx \
 --user=nginx --group=nginx --with-http_stub_status_module
//指定Nginx服務的安裝目錄、運行用戶及組
啟用http_stub_status_module模塊支持狀態(tài)統(tǒng)計,便于查看服務器的連接信息
[root@localhost nginx-1.12.0]# make && make install
4)優(yōu)化路徑
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin

2.Nginx服務的運行控制

1)檢查Nginx服務配置文件

與Apache的主程序httpd相似,Nginx也是使用“-t”選項對其配置文件進行語法檢查。若要檢查位于其他位置的配置文件,可使用“-c”選項來指定路徑。

[root@localhost nginx-1.12.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
2)啟動、停止Nginx服務
[root@localhost ~]# nginx
//直接運行nginx命令即可啟動Nginx服務
[root@localhost ~]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      46231nginx: master 
//默認也是TCP協(xié)議80端口,如果有其他Web服務軟件,應修改其端口,避免沖突
[root@localhost ~]# lynx http://127.0.0.1
//可以使用lynx命令進行文本瀏覽器進行檢查(需要安裝lynx軟件包)
[root@localhost ~]# killall -s HUP nginx
//重新加載Nginx配置文件
[root@localhost ~]# killall -s QUIT nginx
//停止Nginx服務
3)添加Nginx服務為系統(tǒng)服務

為了使Nginx服務的啟動、停止、重載等操作更加方便,可以編寫Nginx服務腳本。腳本內(nèi)容如下:

[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
PROG="/usr/local/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
        $PROG
;;
stop)
        kill -s QUIT $(cat $PIDF)
;;
restart)
        $0 stop
        $0 start
;;
reload)
        kill -s HUP $(cat $PIDF)
;;
*)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
esac
        exit 0

[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# systemctl start nginx
//即可使用systemctl工具來管理Nginx服務

3.Nginx服務配置文件詳解

Nginx服務的主配置文件為:/usr/local/nginx/conf/nginx.conf。

1)全局配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#user  nginx;                                               //運行用戶
worker_processes  1;                                 //工作進程數(shù)量

#error_log  logs/error.log;                           //錯誤日志文件的位置
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;                             //PID文件的存放位置

worker_processes配置項表示工作進程的數(shù)量,若服務器有多塊CPU或者使用多核處理器,可以參考CPU核心總數(shù)來指定工作進程數(shù)(可以使用命令cat /proc/cpuinfo | grep "processor" | wc -l
);如果網(wǎng)站訪問量需求不大,一般設為1即可(根據(jù)情況自行調(diào)整)。

2)I/O事件配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
                    …………………………                 //省略部分內(nèi)容
events {
    use epoll;                                                     //使用epoll模型
    worker_connections  1024;                         //每個進程處理1024個連接
}

對于2.6版本及以上的內(nèi)核,建議使用epoll模型來提高性能。

3)HTTP配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
                    …………………………                 //省略部分內(nèi)容
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 {                                                    //Web服務的監(jiān)聽配置
        listen       80;                                       //監(jiān)聽地址及端口
        server_name  localhost;                     //網(wǎng)站名稱
        charset utf-8;                                      //網(wǎng)頁的默認字符集
        location / {                                          //根目錄配置(必須存在)
            root   html;                                     //網(wǎng)站根目錄的位置
            index  index.html index.php;         //默認首頁
        }
        error_page   500 502 503 504  /50x.html;              //內(nèi)部錯誤的反饋頁面
        location = /50x.html {                                              //錯誤頁面配置
            root   html;
        }

    }
}

root語句用來設置特定訪問位置的網(wǎng)頁文檔路徑,默認為Nginx安裝目錄下的html子目錄。根據(jù)實際情況自行修改。

三、訪問狀態(tài)統(tǒng)計及虛擬主機應用

1.Nginx的訪問狀態(tài)統(tǒng)計

Nginx內(nèi)置了HTTP_STUB_STATUS狀態(tài)統(tǒng)計模塊,用來反饋當前的Web訪問情況,編譯安裝Nginx時需添加--with-http_stub_status_module來啟動模塊。除此之外,還要次改配置文件:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
                         ……………………                   //省略部分內(nèi)容
server {
…………           //省略部分內(nèi)容,添寫以下內(nèi)容
 location /status{
        stub_status on;
        access_log  off;
        }
}
[root@localhost ~]# systemctl restart nginx

用戶測試訪問:
Nginx虛擬主機相關配置詳解
其中“Active connections”表示當前的活動連接數(shù);
“server accepts handled requests”表示已處理的連接信息:
三個數(shù)字依次表示已處理的連接數(shù)、成功的TCP握手次數(shù)、已處理的請求數(shù)。

2.基于域名的虛擬Web主機

基于域名的虛擬Web主機通過域名區(qū)分不同的Web站點。使用Nginx搭建虛擬主機服務器時,每個虛擬Web站點擁有獨立的“server{}”配置段各自的IP地址、端口號、域名都可自行指定。本次示例創(chuàng)建用于不同域名的虛擬主機。

創(chuàng)建虛擬主機的基本步驟:

(1)搭建DNS服務,使兩個域名解析為同一IP地址。

搭建DNS服務可以參考博文:Linux搭建DNS服務

(2)準備網(wǎng)站目錄及測試文件
[root@localhost ~]# mkdir -p /var/www/benet
[root@localhost ~]# mkdir -p /var/www/accp
[root@localhost ~]# echo "www.benet.com" > /var/www/benet/index.html
[root@localhost ~]# echo "www.accp.com" > /var/www/accp/index.html
(3)調(diào)整Nginx服務的主配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
              ………………                    //省略部分內(nèi)容
http {
              ………………                    //省略部分內(nèi)容
  server {
        listen       80;
        server_name  www.benet.com;
        charset utf-8;
        location / {
            root   /var/www/benet;
            index  index.html index.php;
        }
        location /status{
        stub_status on;
        access_log  off;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
    server {
        listen       80;
        server_name  www.accp.com;
        charset utf-8;
        location / {
            root   /var/www/accp;
            index  index.html index.php;
        }
        location /status{
        stub_status on;
        access_log  off;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}
[root@localhost ~]# systemctl restart nginx
(4)訪問虛擬主機

Nginx虛擬主機相關配置詳解
Nginx虛擬主機相關配置詳解

———————— 本文至此結(jié)束,感謝閱讀 ————————

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI