linux(centos7) 安裝nginx

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

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

學習來源:java

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

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

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

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.gzcentos

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./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 的模塊文檔中找到詳細的配置說明。

相關文章
相關標籤/搜索