今天我們來介紹一下高性能的http服務器端軟件nginx的部署與配置,具體什么是nginx,我這里就先不聊了,感興趣的同學可以自行去了解。
1.nginx的安裝
1.yum安裝和源碼編譯的安裝的不同點
目前安裝nginx通常會使用兩種方法,1.通過操作系統的包管理器進行安裝; 2.在官方網站下載源碼,編譯安裝。
下面我們來了解一下兩者的區別。
目前在官方的版本種,有兩個大方向,一個是開源免費版的nginx,另一個是商業版nginx plus,這兩個版本分別有不同的官方網站,如下:
開源版官網:nginx.org
商業版官網:nginx.com
我們這里主要介紹開源版,在開源版下也細分成三個小類:"主線版本”,“穩定版本”,“歷史版本”。
我們可以打開如下官方連接,查看上述各個版本的信息。
http://nginx.org/en/download.html
從上圖我們看出,在當前時間下,nginx的最新版的版本是1.17.10,穩定版本是1.18.0 這里可能會有人奇怪1.17是最新版,1.18還是穩定版?這個就跟nginx他們自己定義有關系了,我們這里不用在這里糾結,在上面我們還看到一些歷史版本的版本號。
2.yum的方式安裝
目前有很多yum源可供我們進行安裝nginx,比如我個人通常會使用阿里的epel源,或者使用nginx官方的yum源,此處已nginx官網的yum源為例,訪問如下官網鏈接可以查看yum源的配置過程。
http://nginx.org/en/linux_packages.html
從上述鏈接中可以找到主線版和穩定版的yum源,此處,我們配置穩定版的官方yum源,配置過程如下:
首先,添加nginx官方yum源文件,此處創建的源文件為/etc/yum.repos.d/nginx.repo,文件內容如下
[root@2-no yum.repos.d]# cat nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1
官網源配置完成后,使用如下命令安裝
[root@2-no rpm]# yum install -y nginx
完成上述配置即可成功安裝nginx,很簡單吧。
3.編譯安裝
首先,從官網下載對應的源碼包,訪問官網的下載頁面,連接如下
http://nginx.org/en/download.html
從上述鏈接中找到你要下載的nginx版本,此處我下載穩定版nginx-1.18.0,並將下載后的包解壓
#首先我們下載安裝一些依賴包,安裝哪些依賴取決於你安裝了哪些模塊,不同的模塊依賴的包不同
[root@test-no ~]# yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed [root@test-no src]# wget http://nginx.org/download/nginx-1.18.0.tar.gz [root@test-no src]# tar -zxvf nginx-1.18.0.tar.gz #進入解壓目錄,查看其內部的文件結構,如下
[root@test-no src]# cd nginx-1.18.0/ [root@test-no nginx-1.18.0]# ll total 760 drwxr-xr-x 6 nexus mysql 326 Jun 1 11:17 auto -rw-r--r-- 1 nexus mysql 302863 Apr 21 22:09 CHANGES #當前版本的nginx都修復了哪些bug、做了哪些變更、新增了哪些功能,在這里,下面的.ru是俄文版 -rw-r--r-- 1 nexus mysql 462213 Apr 21 22:09 CHANGES.ru drwxr-xr-x 2 nexus mysql 168 Jun 1 11:17 conf #conf目錄中的文件是一些nginx配置文件的模板文件,編譯安裝時這些文件最終會被拷貝到安裝目錄中 -rwxr-xr-x 1 nexus mysql 2502 Apr 21 22:09 configure #此目錄中的configure腳本文件,進行一些編譯安裝的相關設置 drwxr-xr-x 4 nexus mysql 72 Jun 1 11:17 contrib #contrib目錄中的vim目錄中的文件可以幫助我們在使用vim編輯nginx配置文件時提供語法高亮功能。 drwxr-xr-x 2 nexus mysql 40 Jun 1 11:17 html #html目錄中的文件是一些靜態頁面的模板文件 -rw-r--r-- 1 nexus mysql 1397 Apr 21 22:09 LICENSE drwxr-xr-x 2 nexus mysql 21 Jun 1 11:17 man #man目錄是幫助文檔的目錄 -rw-r--r-- 1 nexus mysql 49 Apr 21 22:09 README drwxr-xr-x 9 nexus mysql 91 Jun 1 11:17 src #src目錄是源代碼目錄 #看看當前configure腳本中都支持哪些參數,執行如下命令,可以看到編譯安裝nginx時我們可以使用的配置選項
[root@test-no nginx-1.18.0]# ./configure --help
執行"./configure --help"命令后,你可以看到很多選項信息,此處就不列出這些信息了,最常用的莫過於--prefix選項了,使用--prefix選項可以指定nginx編譯安裝的目錄,
除了一些常見的目錄設置選項,你應該還會看到好多類似"--with-XXX_module "或" --without-XXX_module"的選項,這些選項是什么意思呢?說到這些選項,又要提到我們剛
才介紹的概念了,那就是"模塊"的概念,我們已經知道,nginx是高度模塊化的,每個模塊就代表一個功能,而且剛才也提到過,只有編譯安裝的方式才能指定安裝哪些模塊,不安裝
哪些模塊,其實,你可以這樣理解,"--with-XXX_module "選項表示指明安裝對應的模塊," --without-XXX_module"選項表示指明不安裝對應的模塊。 [root@test-no src]# mkdir -p nginx
#現在開始執行configure 腳本,並且啟用一些可能會用到的常用模塊,操作如下 [root@test-no nginx-1.18.0]# ./configure --prefix=/usr/local/src/nginx --with-file-aio --with-http_auth_request_module --with-http_ssl_module \
--with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_geoip_module=dynamic \
--with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module \
--with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module \
--with-http_perl_module=dynamic --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module
#上面的腳本執行完之后沒報錯的話,就執行下面的命令進行編譯安裝
[root@test-no nginx-1.18.0]# make && make instal
#nginx命令簡化方法
[root@test-no nginx]# echo 'export PATH=/usr/local/src/nginx/sbin:$PATH'>>/etc/profile
[root@test-no nginx]# source /etc/profile
[root@test-no nginx]# which nginx
/usr/local/src/nginx/sbin/nginx
2.nginx的相關配置
1.配置文件語法高亮配置
通過上述兩種方法安裝nginx以后,當你使用vim編輯器編輯nginx的配置文件時,vim編輯器是無法自動識別出nginx的相關語法的,所以,使用vim編輯器編輯nginx配置文件時,無法實現"語法高亮"功能,也就是說,默認情況下,使用vim編輯nginx配置文件時,沒有彩色的語法着色,對於使用者來說,這樣體驗不好,nginx官方很貼心,在源碼包中為我們提供了vim針對nginx的語法高亮配置文件,我們只要把這些文件拷貝到vim的對應目錄中即可直接使用,方法很簡單,如下
#首先我們進入到源碼包解決目錄下,將相應的語法文件拷貝到對應的目錄中,即可完成 [root@test-no nginx-1.18.0]# cp -r contrib/vim/* /usr/share/vim/vimfiles/
無論是通過官方yum源的方式還是通過編譯安裝的方式,都可以使用上述方法實現nginx的語法高亮,如果你跟我一樣,習慣使用阿里鏡像源中的epel源安裝nginx,那么你會發現,通過epel源安裝nginx后,會自動完成上述vim語法文件的配置過程,不用我們手動拷貝對應的文件,對應的語法文件也是安裝包自帶的,也很是方便的。
2.nginx目錄結構
[root@test-no ~]# cd /usr/local/src/nginx [root@test-no nginx]# ll total 0 drwxr-xr-x 2 root root 333 Jun 2 10:22 conf #存放了nginx相關的配置文件 drwxr-xr-x 2 root root 40 Jun 2 09:59 html #默認提供的web服務的"根目錄" drwxr-xr-x 2 root root 6 Jun 2 09:59 logs #nginx日志的存放目錄 drwxr-xr-x 2 root root 135 Jun 2 09:59 modules #存放了一些模塊會用到的庫 drwxr-xr-x 2 root root 19 Jun 2 09:59 sbin #存放了nginx的二進制文件,我們需要使用nginx二進制文件啟動nginx
3.nginx.conf 配置文件說明
[root@test-no nginx]# cd conf/
#為了演示方便,我刪除了一些沒用的空行,最終,nginx.conf中真正有用的部分如下: [root@test-no conf]# sed -i '/^[[:space:]]*#/'d nginx.conf [root@test-no conf]# cat nginx.conf worker_processes 1; ← worker 進程數量 events { ←事件區塊 worker_connections 1024; ←每個worker進程可以處理的連接數 } ←事件區塊結束 http { ← HTTP 區塊 include mime.types; ←支持的媒體文件 default_type application/octet-stream;←默認的媒體類型 sendfile on; ←高效傳輸模式 keepalive_timeout 65; ←超時時間 server { ← server 區塊 listen 80; ←端口 server_name localhost; ←域名 location / { ←第一個location區塊 root html; ←站點目錄 index index.html index.htm; ←首頁文件 } ←第一個location區塊結束 error_page 500 502 503 504 /50x.html; ← 錯誤信息配置 location = /50x.html { 文件位置 root html; 在哪找:路徑 } } ← server 區塊結束 } ← HTTP 區塊結束
從上述語法配置示例可以看出,上述示例可以分為幾個邏輯部分,http部分、server部分、location部分,或者說,上述示例可以分為幾個邏輯塊,http塊、server塊、location塊,聰明如你一定看出來了,每個"配置塊"都是使用大括號"{ }"作為分界線的,而且,從縮進可以看出,它們是有層級關系的,http中可以配置多個server,一個server中可以配置多個location,我們知道,nginx最基礎的功能就是用來提供http服務,所以,跟http有關的公共配置,可以放置在http塊中,http塊中又可以配置多個server,那么server代表了什么呢?我們在一台主機中安裝了nginx,那么能不能讓這台nginx主機同時提供多個web服務呢?答案是肯定的,每一個server就代表一個http服務,我們可以同時配置多個server,以便同時提供多個http服務,不同的server可以使用不同的配置,寫入到某個server塊中的配置只對對應的http服務生效,如果多個server存在共同的公用配置,則可以將共同的配置寫在http塊中,以便多個server共享這些配置,一個server塊中又可以有一個或多個location,location又是什么意思呢?當我們訪問一個網絡上的資源時,都是通過url訪問的,你可以把location當做url的一部分,此處,我們使用如下url作為示例:
http://www.zsythink.net/archives/1591
上述鏈接中的"/archives"部分就是一個location,我們可以通過location將url中的路徑和服務器的某個目錄建立起關聯關系,此處不用糾結,在用到它時我們再來細說。
4.yum安裝和編譯安裝后的區別
編譯安裝后,我們通過如下二進制文件啟動nginx
/nginx安裝路徑/sbin/nginx
通過yum源安裝nginx后,對應的二進制文件已經自動拷貝到了/usr/sbin目錄中,而此目錄默認已經加入到環境變量中,所以我們可以在任意目錄直接執行"nginx"命令啟動nginx
編譯安裝后,nginx相關的配置文件存放在如下路徑中
/nginx安裝路徑/conf/
而通過yum源安裝nginx后,相關的配置文件存放在如下路徑中
/etc/nginx/
通過編譯安裝的方式安裝nginx以后,默認的"server塊"直接配置到了nginx.conf文件中,而通過官方yum源安裝nginx后,默認的"server塊"會配置在"/etc/nginx/conf.d/default.conf"文件中,換句話說就是,如果你通過官方yum源安裝了nginx,那么你在/etc/nginx/nginx.conf文件中無法找到默認提供的server塊,只能在/etc/nginx/conf.d/default.conf文件中找到默認的server塊,這是為什么呢?其實,我們從/etc/nginx/nginx.conf文件中就能找到答案,打開/etc/nginx/nginx.conf文件,查看默認的http塊的配置,你會在http塊中發現如下配置指令
include /etc/nginx/conf.d/*.conf;
出現上述情況的根本原因就在於這條配置指令,include指令表示將指定的文件中的內容包含到當前位置中,舉個例子,如果我在http配置塊中的第三行設置了"include test.conf;"指令,那么test.conf文件中的所有內容都會替換到http配置塊的第三行,換句話說就是,雖然文本內容寫在test.conf文件中,但是通過include指令替換后,相當於test.conf中的文本寫在了http塊中的第三行。
那么,理解了include指令的作用以后,再回過頭看剛才的配置,就一目了然了。
"include /etc/nginx/conf.d/*.conf;"這條指令的作用就是將"/etc/nginx/conf.d/*.conf"文件中的內容替換到當前指令所在的位置,由於使用了"*"作為通配符,所以/etc/nginx/conf.d/目錄中所有以".conf"作為后綴名的文件都會被匹配到,/etc/nginx/conf.d/default.conf文件自然也會被匹配到,所以最終,http塊中include指令所在位置的文本內容會被替換成/etc/nginx/conf.d/default.conf文件中的內容,而/etc/nginx/conf.d/default.conf文件中的內容恰好是默認提供的server塊配置,當然,如果你在/etc/nginx/conf.d/目錄中放了一些其他以".conf"結尾的文本文件,那么這些文件中的內容也會因為剛才的include指令而被包含到nginx.conf文件中。
綜上所述,通過官方yum源安裝nginx后,雖然/etc/nginx/nginx.conf文件中看不到默認server塊的配置,但是並不代表沒有提供默認的server塊配置,只是通過include指令的方式,將默認server塊的配置單獨放置在了/etc/nginx/conf.d/default.conf文件中,最終效果與直接將server塊配置寫入到http塊中無異。其實,即使是通過編譯安裝的方式安裝的nginx,也可以借鑒這種配置方式,當我們配置了多個server塊時,可以將各個server塊分別提取出來,單獨放置到一個配置文件中,然后再使用include指令引用對應的配置文件,這樣就能從邏輯上將各個server塊的配置隔離到不同的配置文件中,結構上更加分明,也方便我們進行配置管理,這些操作在以后都會進行實際的演示,不必着急,此時,你只要搞明白大概的原理即可。
通過官方yum安裝后,如果你查看了/etc/nginx/conf.d/default.conf中提供的默認server塊配置,那么你會發現,默認server塊中的location塊配置的root指令對應的路徑是"/usr/share/nginx/html",也就是說,默認提供的server對應的文檔根目錄是此路徑,我們需要將對應的資源放置到此目錄中,即可通過默認的服務進行訪問了。
如果你沒有使用官方yum源,而是使用了epel源安裝了nginx,那么配置文件和默認的index.html頁面可能又是另外一番樣子了,但是只要搞明白原理,都是可以輕松應對的。
3.常見錯誤及解決辦法
1 .nginx軟件安裝過程中遇到的問題
#執行configure腳本 [root@test-no nginx-1.18.0]# ./configure --prefix=/usr/local/src/nginx --with-file-aio --with-http_auth_request_module --with-http_ssl_module \ --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_geoip_module=dynamic \ --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module \ --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module \ --with-http_perl_module=dynamic --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module #這里執行完之后我們會發現類似下面的錯誤,這里就是上面講的,我們要安裝的模塊缺少某些依賴包沒裝。 ./configure: error: the GeoIP module requires the GeoIP library. You can either do not enable the module or install the library. #解決依賴后再執行一下上面的configure腳本 [root@test-no nginx-1.18.0]# yum install -y GeoIP-devel
2.nginx軟件啟動過程中遇到的問題
nginx軟件重復啟動產生的錯誤信息
[root@test-no ~]# nginx nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] still could not bind()
解決方法:
nginx軟件已經啟動無需反復啟動,如果需要重新啟動需要停止nginx進程或者用reload方式進行重啟
這里還有一個情況就是其他服務依賴的nginx占用了80端口,例如gitlab服務,這個時候我們就需要修改端口,來解決這個問題
3.啟動Nginx 時如下報錯"nginx:[emerg]getpwnam(“nginx”〉failed"
解答這是因為沒有對應的Nginx服務用戶,執行useradd nginx-s/sbin/no丨ogin-M創建 Nginx用戶即可
4.nginx軟件編譯安裝后,看不到程序目錄(/application)
說明:編譯安裝步驟不對(配置 編譯 編譯安裝生成/appliation)
5.nginx軟件排查問題三部曲說明
a 在客戶端上ping服務器端IP,檢查鏈路是否通暢
b 在客戶端上telnet服務器端IP、端口,檢查鏈路訪問是否通暢
c 在客戶端上wget檢測模擬頁面訪問是否正常
6.【注意】403狀態碼出現情況原因
01. 服務阻止客戶端訪問
02. 服務端站點目錄中,沒有指定首頁文件信息
4.nginx軟件使用命令參數
下面的nginx一般使用絕對路徑,但是如果你配置了環境變量,直接nginx也可以
1.nginx 啟動方法
[root@test-no ~]# nginx
2.nginx停止方法
[root@test-no ~]# nginx -s stop
使用"-s"選項除了能夠發送stop信號,還能發送quit信號、reopen信號以及reload信號,那么這三種信號分別代表什么意思呢?我們一一道來
quit信號:與stop信號的作用類似,quit信號作用也是用於停止nginx服務,quit信號和stop信號的區別在於,nignx進程收到stop信號以后會立即停止服務,而收到quit信號后,不會再接收新的請求,但是會先處理完已經接受的鏈接請求,處理完這些請求之后再停止服務,這種停止方式被稱之為"優雅的停止"。
reload信號:reload信號的作用就是在不停止服務的情況下重載配置文件,比如,nginx正在正常的提供服務,此時,管理員修改了nginx.conf文件中的配置指令,管理員希望新的配置立刻生效,但是又不希望重啟nginx服務,此時就可以使用"nginx -s reload"命令重載配置文件,以便在不重啟nginx的情況下載入新的配置,同時避免了因重啟而造成的服務中斷。
reopen信號:利用reopen信號可以使nignx進程重新打開日志文件,以便實現日志分割的效果
3.nginx重啟方法(平滑重啟)
[root@test-no ~]# nginx -s reload
#這個命令執行的前提是nginx是啟動的,不然會報找不到pid文件
4.檢查配置文件語法是否正確
[root@test-no ~]# nginx -t nginx: the configuration file /usr/local/src/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/src/nginx/conf/nginx.conf test is successful
5.顯示配置參數 -V (大寫V)
[root@test-no ~]# nginx -V nginx version: nginx/1.18.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/src/nginx --with-file-aio --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module
6.nginx軟件使用過程中深入說明
①. nginx軟件語法檢查方法:
nginx -t
②. nginx軟件訪問測試過程:
curl -v www.baidu.com
③. nginx軟件編譯參數查看:
nginx -V <--- 查看原有的編譯參數信息
5.虛擬主機配置
1.利用nginx服務搭建文件共享服務器
通過配置 autoindex on; 參數
使用 autoindex參數,nginx能識別的直接顯示,不識別的直接下載
配置完 autoindex on; 參數以后 會顯示站點下的文件信息
對於nginx可以解析的資源會解析相應的內容
對於nginx不可以解析的資源會直接下載
[root@test-no ~]# cat /usr/local/src/nginx/conf/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 80; server_name www.qingbai.com; location / { root html/www; # index index.html index.htm; autoindex on; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
[root@test-no ~]# nginx -t
nginx: the configuration file /usr/local/src/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/src/nginx/conf/nginx.conf test is successful
[root@test-no ~]# nginx -s reload
#創建測試文件和目錄
[root@test-no ~]# mkdir /usr/local/src/nginx/html/www/test1 -p
[root@test-no ~]# mkdir /usr/local/src/nginx/html/www/test2 -p
[root@test-no ~]# mkdir /usr/local/src/nginx/html/www/test3 -p
[root@test-no ~]# touch /usr/local/src/nginx/html/www/test4
接下來修改本機的hosts文件,然后訪問測試
2.基於域名的虛擬主機
[root@test-no ~]# cat /usr/local/src/nginx/conf/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 80; server_name www.test1.com; location / { root html/test1; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name www.test2.com; location / { root html/test2; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name www.test3.com; location / { root html/test3; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@test-no ~]# nginx -t nginx: the configuration file /usr/local/src/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/src/nginx/conf/nginx.conf test is successful
創建站點目錄
[root@test-no nginx]# for name in test1 test2 test3 ;do mkdir html/$name -p ;done
創建主頁文件
[root@test-no nginx]# for name in test1 test2 test3 ;do echo "web01 $name" > html/$name/index.html ;done
檢查主頁內容信息
[root@test-no nginx]# for name in test1 test2 test3 ;do cat html/$name/index.html ;done web01 test1 web01 test2 web01 test3
修改主機hosts文件
[root@test-no nginx]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.31.46.3 www.test1.com www.test2.com www.test3.com
測試
[root@test-no nginx]# curl www.test1.com web01 test1 [root@test-no nginx]# curl www.test2.com web01 test2 [root@test-no nginx]# curl www.test3.com web01 test3
3.基於端口的虛擬主機
[root@test-no nginx]# cat /usr/local/src/nginx/conf/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 80; server_name www.test1.com; location / { root html/test1; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 81; server_name www.test1.com; location / { root html/test2; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@test-no nginx]# nginx -t nginx: the configuration file /usr/local/src/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/src/nginx/conf/nginx.conf test is successful [root@test-no nginx]# nginx -s reload
檢查端口信息
[root@test-no nginx]# netstat -lntup |grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10528nginx: master tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 10528nginx: master
測試訪問
[root@test-no nginx]# curl www.test1.com:80 web01 test1 [root@test-no nginx]# curl www.test1.com:81 web01 test2
4.基於IP的虛擬主機
[root@test-no ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether fa:16:3e:e6:3e:3e brd ff:ff:ff:ff:ff:ff inet 172.31.46.3/24 brd 172.31.46.255 scope global dynamic eth0 valid_lft 27830sec preferred_lft 27830sec inet6 fe80::f816:3eff:fee6:3e3e/64 scope link valid_lft forever preferred_lft forever #首先我們添加兩個IP
[root@test-no ~]# ifconfig eth0:0 172.31.46.10 netmask 255.255.255.0 up #eth0表示網卡名字,eth0:0中的后面那個0,
是代表第幾個新增加的ip,如0,1,2,3, 172.31.46.10是新增加的ip地址,up表示立即激活 [root@test-no ~]# ifconfig eth0:1 172.31.46.11 netmask 255.255.255.0 up [root@test-no ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether fa:16:3e:e6:3e:3e brd ff:ff:ff:ff:ff:ff inet 172.31.46.3/24 brd 172.31.46.255 scope global dynamic eth0 valid_lft 27669sec preferred_lft 27669sec inet 172.31.46.10/24 brd 172.31.46.255 scope global secondary eth0:0 valid_lft forever preferred_lft forever inet 172.31.46.11/24 brd 172.31.46.255 scope global secondary eth0:1 valid_lft forever preferred_lft forever inet6 fe80::f816:3eff:fee6:3e3e/64 scope link valid_lft forever preferred_lft forever
#注意:這個設置之后,重啟會失效
不失效的方法是將上面命令加入到文件/etc/rc.d/rc.local里面
[root@test-no ~]# cat /usr/local/src/nginx/conf/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 172.31.46.10:80; server_name www.test1.com; location / { root html/test1; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 172.31.46.11:80; server_name www.test1.com; location / { root html/test2; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@test-no ~]# nginx -t nginx: the configuration file /usr/local/src/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/src/nginx/conf/nginx.conf test is successful #重啟服務,注意使用的是直接重啟的方式
#只要nginx配置文件中涉及到IP地址的更改只能正真的重啟才生效
[root@test-no ~]# nginx -s stop [root@test-no ~]# nginx [root@test-no ~]# netstat -lntup |grep ng tcp 0 0 172.31.46.11:80 0.0.0.0:* LISTEN 14856nginx: master tcp 0 0 172.31.46.10:80 0.0.0.0:* LISTEN 14856nginx: master [root@test-no ~]# curl 172.31.46.11 web01 test2 [root@test-no ~]# curl 172.31.46.10 web01 test1