linux(centos7) 安裝nginx


linux(centos7) 安裝nginx 1.14(stable) 版本

Nginx配置文件常見結構的從外到內依次是「http」「server」「location」等等,缺省的繼承關系是從外到內,也就是說內層塊會自動獲取外層塊的值作為缺省值。

學習來源:

https://blog.csdn.net/wxyjuly/article/details/79443432

https://blog.csdn.net/tangyaliang11/article/details/78675535

https://blog.csdn.net/wxyjuly/article/details/79443432

http://blog.51cto.com/3241766/2094315

安裝nginx zlib pcre

wget nginx.org/download/nginx-1.14.0.tar.gz

wget zlib.net/zlib-1.2.11.tar.gz

wget https://ftp.pcre.org/pub/pcre/pcre2-10.31.tar.gz

wget https://www.openssl.org/source/openssl-1.1.1-pre9.tar.gz

 

yum --disablerepo=\* --enablerepo=c6-media install gcc gcc-c++ openssl openssl-devel cyrus-sasl-md5

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre=../pcre2-10.31 --with-zlib=../zlib-1.2.11 --with-openssl=./openssl-1.1.1-pre9

 ./configure --prefix=/usr/local/nginx  --with-pcre=../pcre2-10.31 --with-zlib=../zlib-1.2.11 --with-openssl=./openssl-1.1.1-pre9

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module --with-pcre=../pcre2-10.31 --with-zlib=../zlib-1.2.11 --with-openssl=./openssl-1.1.1-pre9

由於安裝的時候,各種依賴有問題 一直安裝不成功;所以選擇其他 用yum來安裝

yum安裝

打開終端安裝依賴軟件
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

 

cd 到 /usr/local目錄下
//下載軟件
wget http://nginx.org/download/nginx-1.13.7.tar.gz

//解壓
tar zxvf nginx-1.13.7.tar.gz

//創建安裝目錄
mkdir -p /usr/local/nginx

//修改配置
cd nginx-1.13.7/
./configure --prefix=/usr/local/nginx

//安裝
make && make install

進入安裝目錄
cd /usr/local/nginx/sbin

啟動
./nginx 

如果遠程訪問的話需要關閉防火牆或者將80端口開放,添加新端口后需要reload 防火牆。

關閉防火牆:

CentOS 7.0默認使用的是firewall作為防火牆。

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall開機啟動

firewall-cmd --state #查看默認防火牆狀態(關閉后顯示notrunning,開啟后顯示running)

 

開放端口:

添加  firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,沒有此參數重啟后失效)

重新載入 firewall-cmd --reload

查看 firewall-cmd --zone= public --query-port=80/tcp

刪除 firewall-cmd --zone= public --remove-port=80/tcp --permanent

 

 

Nginx配置文件常見結構的從外到內依次是「http」「server」「location」等等,缺省的繼承關系是從外到內,也就是說內層塊會自動獲取外層塊的值作為缺省值。

 

配置

 


 

以上安裝方法nginx的配置文件位於

 

/usr/local/nginx/conf/nginx.conf
Nginx配置文件常見結構的從外到內依次是「http」「server」「location」等等,缺省的繼承關系是從外到內,也就是說內層塊會自動獲取外層塊的值作為缺省值。

 

Server

 

接收請求的服務器需要將不同的請求按規則轉發到不同的后端服務器上,在 nginx 中我們可以通過構建虛擬主機(server)的概念來將這些不同的服務配置隔離。

 

server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
}

 

例如我們筆戈玩下的兩個子項目 passport 和 wan 就可以通過在 nginx 的配置文件中配置兩個 server,servername 分別為 passport.bigertech.com 和 wan.bigertech.com。這樣的話不同的 url 請求就會對應到 nginx 相應的設置,轉發到不同的后端服務器上。
這里的 listen 指監聽端口,server_name 用來指定IP或域名,多個域名對應統一規則可以空格分開,index 用於設定訪問的默認首頁地址,root 指令用於指定虛擬主機的網頁跟目錄,這個地方可以是相對地址也可以是絕對地址。
通常情況下我們可以在 nginx.conf 中配置多個server,對不同的請求進行設置。就像這樣:

 

server {
listen 80;
server_name host1;
root html;
index index.html
index.htm;
}
server {
listen 80;
server_name host2;
root /data/www/html;
index index.html index.htm;
}

 

但是當 server 超過2個時,建議將不同對虛擬主機的配置放在另一個文件中,然后通過在主配置文件 nginx.conf 加上 include 指令包含進來。更便於管理。

 

include vhosts/*.conf;

 

就可以把vhosts的文件都包含進去啦。

 

Localtion
每個 url 請求都會對應的一個服務,nginx 進行處理轉發或者是本地的一個文件路徑,或者是其他服務器的一個服務路徑。而這個路徑的匹配是通過 location 來進行的。我們可以將 server 當做對應一個域名進行的配置,而 location 是在一個域名下對更精細的路徑進行配置。

 

以上面的例子,可以將root和index指令放到一個location中,那么只有在匹配到這個location時才會訪問root后的內容:

 

location / {

 

  1. root /data/www/host2;
  2. index index.html index.htm;

 

}

 

location 匹配規則

 

~ 波浪線表示執行一個正則匹配,區分大小寫
~* 表示執行一個正則匹配,不區分大小寫
^~ ^~表示普通字符匹配,如果該選項匹配,只匹配該選項,不匹配別的選項,一般用來匹配目錄
= 進行普通字符精確匹配

 

匹配例子:

 

location = / {
# 只匹配"/". [ configuration A ]
}
location / {
# 匹配任何請求,因為所有請求都是以"/"開始 # 但是更長字符匹配或 者正則表達式匹配會優先匹配 [ configuration B ]
}
location ^~ /images/ {
# 匹配任何以 /images/ 開始的請求,並停止匹配 其它
location [ configuration C ]
}
location ~* .(gif|jpg|jpeg)$ {
# 匹配以 gif, jpg, or jpeg結尾的請求.
# 但是所有 /images/ 目錄的請求將由 [Configuration C]處理.
[ configuration D ]
}
請求:/ -> 符合configuration A
/documents/document.html -> 符合configuration B
/images/1.gif -> 符合configuration C
/documents/1.jpg ->符合 configuration D

 

靜態文件映射
訪問文件的配置主要有 root 和 aliasp’s 兩個指令。這兩個指令的區別容易弄混:
alias
alias后跟的指定目錄是准確的,並且末尾必須加 /。

 

location /c/ {
alias /a/;
}

 

root
root后跟的指定目錄是上級目錄,並且該上級目錄下要含有和location后指定名稱的同名目錄才行。

 

location /c/ {
root /a/;
}

 

如果你需要將這個目錄展開,在這個location的末尾加上「autoindex on; 」就可以了

 

轉發
配置起來很簡單比如我要將所有的請求到轉移到真正提供服務的一台機器的 8001 端口,只要這樣:

 

location / {
proxy_pass 172.16.1.1:8001;
}

 

這樣訪問host時,就都被轉發到 172.16.1.1的8001端口去了。

 

負載均衡

 

upstream myserver; {
ip_hash;
server 172.16.1.1:8001;
server 172.16.1.2:8002;
server 172.16.1.3;
server 172.16.1.4;
}
location / {
proxy_pass http://myserver;
}

 

我們在 upstream 中指定了一組機器,並將這個組命名為 myserver,這樣在 proxypass 中只要將請求轉移到 myserver 這個 upstream 中我們就實現了在四台機器的反向代理加負載均衡。其中的 ip_hash 指明了我們均衡的方式是按照用戶的 ip 地址進行分配。另外還有輪詢、指定權重輪詢、fair、url_hash幾種調度算法。

 

總結

 

以上是最簡單的通過 nginx 實現靜態文件轉發、反向代理和負載均衡的配置。在 nginx 中所有的功能都是通過模塊來實現的,比如當我們配置 upstream 時是用 upstream 模塊,而 server 和 location 是在 http core 模塊,其他的還有流控的 limt 模塊,郵件的 mail 模塊,https 的 ssl 模塊。他們的配置都是類似的可以再 nginx 的模塊文檔中找到詳細的配置說明。

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM