天天看點

代理伺服器?nginx是什麼?

Nginx

代理:

代理伺服器,客戶機在發送請求時,不會直接發送給目的主機,而是先發送給代理伺服器,代理服務接受客戶機請求之後,再向主機發出,并接收目的主機傳回的資料,存放在代理伺服器的硬碟中,再發送給客戶機。

正向代理:

正向代理,架設在客戶機與目标主機之間,隻用于代理内部網絡對 Internet 的連接配接請求,客戶機必須指定代理伺服器,并将本來要直接發送到 Web 伺服器上的 Http 請求發送到代理伺服器中。

反向代理:

反向代理伺服器架設在伺服器端,通過緩沖經常被請求的頁面來緩解伺服器的工作量,将客戶機請求轉發給内部網絡上的目标伺服器;并将從伺服器上得到的結果傳回給 Internet 上請求連接配接的用戶端,此時代理伺服器與目标主機一起對外表現為一個伺服器。

虛拟主機:

虛拟主機是一種特殊的軟硬體技術,它可以将網絡上的每一台計算機分成多個虛拟主機,每個虛拟主機可以獨立對外提供 www 服務,這樣就可以實作一台主機對外提供多個 web 服務,每個虛拟主機之間是獨立的,互不影響的。

通過 Nginx 可以實作虛拟主機的配置,Nginx 支援三種類型的虛拟主機配置

  • 基于 IP 的虛拟主機
  • 基于域名的虛拟主機
  • 基于端口的虛拟主機

nginx的使用:

Nginx 是一款高性能的 HTTP 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器。由俄羅斯的程式設計師 Igor Sysoev 所開發,官方測試 Nginx 能夠支支撐 5 萬并發連結,并且 CPU、記憶體等資源消耗卻非常低,運作非常穩定。

Nginx既可以在内部的直接支援Rails和PHP程式對外進行服務,也可以支援HTTP代理服務對外進行服務,采用C語言編寫,處理靜态檔案,索引檔案以及自動索引;打開檔案描述符緩沖。無緩存的反向代理加速,簡單的負載均衡和容錯。FastCGI,簡單的負載均衡和容錯。子產品化的結構。包括 gzipping, byte ranges, chunked responses,以及 SSI-filter 等 filter。如果由 FastCG或其它代理伺服器處理單頁中存在的多個 SSI,則這項處理可以并行運作,而不需要互相等待。支援 SSL 和 TLSSNI。

Nginx 的應用場景

  • HTTP 伺服器:Nginx 是一個 HTTP 服務可以獨立提供 HTTP 服務。可以做網頁靜态伺服器。
  • 虛拟主機:可以實作在一台伺服器虛拟出多個網站。例如個人網站使用的虛拟主機。
  • 反向代理,負載均衡:當網站的通路量達到一定程度後,單台伺服器不能滿足使用者的請求時,需要用多台伺服器叢集可以使用 Nginx 做反向代理。并且多台伺服器可以平均分擔負載,不會因為某台伺服器負載高當機而某台伺服器閑置的情況 .

    負載均衡,英文名稱為 Load Balance,其意思就是分攤到多個操作單元上進行執行,例如 Web 伺服器、FTP 伺服器、企業關鍵應用伺服器和其它關鍵任務伺服器等,進而共同完成工作任務

1.docker-compose.yml來使用nginx:

version: '3.1'
  services:
  nginx:
    restart: always
    image: nginx
    container_name: nginx
    ports:
      - 81:80
    volumes:
      - ./conf/nginx.conf:/etc/nginx/nginx.conf
      - ./wwwroot:/usr/share/nginx/wwwroot
 # 配置nginx的資料卷:
  在dokcer檔案下建立:nginx的目錄下建立:nginx.conf檔案:
  在目前目錄建立wwwroot目錄
  這裡直接書寫為
  /usr/local/dokcer/nginx/conf/nginx.conf
  /usr/local/dokcer/nginx/wwwroot
  在nginx.conf目錄配置nginx的虛拟主機:
           

2.nginx中使用虛拟主機配置:

需求

Nginx 對外提供 80 和 8080 兩個端口監聽服務

請求 80 端口則請求 html80 目錄下的 html

請求 8080 端口則請求 html8080 目錄下的 html

建立目錄及檔案

在 /usr/local/docker/nginx/wwwroot 目錄下建立 html80 和 html8080 兩個目錄,并分辨建立兩個 index.html 檔案

# 配置虛拟主機
修改 /usr/local/docker/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;
    # 配置虛拟主機 192.168.75.145
    server {
        # 監聽的ip和端口,配置 192.168.75.145:80
        listen       80;
        # 虛拟主機名稱這裡配置ip位址
        server_name  192.168.75.145;
        # 所有的請求都以 / 開始,所有的請求都可以比對此 location
        location / {
            # 使用 root 指令指定虛拟主機目錄即網頁存放目錄
            # 比如通路 http://ip/index.html 将找到 /usr/local/docker/nginx/wwwroot/html80/index.html
            # 比如通路 http://ip/item/index.html 将找到 /usr/local/docker/nginx/wwwroot/html80/item/index.html
​
            root   /usr/share/nginx/wwwroot/html80;
                # 指定歡迎頁面,按從左到右順序查找
            index  index.html index.htm;
        }
    }
    # 配置虛拟主機 192.168.75.245
    server {
        listen       8080;
        server_name  192.168.75.145;
​
          location / {
            root   /usr/share/nginx/wwwroot/html8080;
            index  index.html index.htm;
          }
    }
}
           
   說明:這裡的啟動的端口必須和dockercompose中的nginx的啟動端口一一對應:
    例如:這裡有兩個分别為8080和80那麼port應這樣寫
    ports:
      - 80:80
      - 8080: 8080
    同時這裡的location不能改變,改變的話也是改變對應的資料卷,其實就是這個檔案映射到資料卷位置
    這裡不需要改變隻需改變html8000這個就可以:
    location / {
      root   /usr/share/nginx/wwwroot/html8080;
      index  index.html index.htm;
    }      

基于域名的虛拟主機配置

需求:兩個域名指向同一台 Nginx 伺服器,使用者通路不同的域名顯示不同的網頁内容 兩個域名是 admin.service.itoken.funtl.com 和 admin.web.itoken.funtl.comNginx 伺服器使用虛拟機 192.168.75.145

配置 Windows Hosts 檔案

通過 host 檔案指定 admin.service.itoken.funtl.com 和 admin.web.itoken.funtl.com 對應 192.168.75.145 虛拟機: 修改 window 的 hosts 檔案:(C:\Windows\System32\drivers\etc)

worker_processes  1;
events {
    worker_connections  1024;
}
​
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    # 配置虛拟主機 192.168.75.145
•    server {
        # 監聽的ip和端口,配置 192.168.75.145:80
        listen       80;
        # 虛拟主機名稱這裡配置ip位址
        server_name  www.kay.com;
        # 所有的請求都以 / 開始,所有的請求都可以比對此 location
        location / {
            # 使用 root 指令指定虛拟主機目錄即網頁存放目錄
            # 比如通路 http://ip/index.html 将找到                      /usr/local/docker/nginx/wwwroot/html80/index.html
            # 比如通路 http://ip/item/index.html 将找到 /usr/local/docker/nginx/wwwroot/html80/item/index.html
            root   /usr/share/nginx/wwwroot/html80;
            # 指定歡迎頁面,按從左到右順序查找
•            index  index.html index.htm;
•        }
•    }
    # 配置虛拟主機 192.168.75.245
    server {
       listen       8080;
       server_name  192.168.75.145;
        location / {
            root   /usr/share/nginx/wwwroot/html8080;
            index  index.html index.htm;
        }
    }
}
           
通過 host 檔案指定 admin.service.itoken.funtl.com 和 admin.web.itoken.funtl.com 對應 192.168.75.145 虛拟機:
這樣通過域名即可通路:
# 建立目錄及檔案
在 /usr/local/docker/nginx/wwwroot 目錄下建立 htmlservice 和 htmlweb 兩個目錄,并分辨建立兩個 index.html 檔案
# 配置虛拟主機
​
      
user  nginx;
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  admin.service.itoken.funtl.com;
        location / {
            root   /usr/share/nginx/wwwroot/htmlservice;
            index  index.html index.htm;
        }
    }
​
    server {
        listen       80;
        server_name  admin.web.itoken.funtl.com;
​
       location / {
             root   /usr/share/nginx/wwwroot/htmlweb;
             index  index.html index.htm;
         }
    }                      
}
           

3.使用nginx反向代理tomcat:

(1) 啟動兩個tomcat:在dokcer-compose.yml 編輯:

      
version: '3'
  services:
    tomcat1:
      image: tomcat
      container_name: tomcat1
      ports:
        - 9090:8080
​
    tomcat2:
      image: tomcat
      container_name: tomcat2
      ports:
        - 9091:8080
​
           
(2) local/docker/nginx/conf 目錄下的 nginx.conf 配置檔案:

      
user  nginx;
worker_processes  1;
​
events {
    worker_connections  1024;
}
​
http {
    include       mime.types;
    default_type  application/octet-stream;
​
    sendfile        on;
​
    keepalive_timeout  65;
    
​
    # 配置一個代理即 tomcat1 伺服器
​
    upstream tomcatServer1 {
        server 192.168.75.145:9090;
    }
​
    #配置一個代理即 tomcat2 伺服器
​
    upstream tomcatServer2 {
        server 192.168.75.145:9091;
    }
    # 配置一個虛拟主機
    server {
        listen 80;
        server_name admin.service.itoken.funtl.com;
        location / {
            #域名 admin.service.itoken.funtl.com 的請求全部轉發到 tomcat_server1 即 tomcat1 服務上
            #可以直接書寫tomcat的路徑即可
            proxy_pass http://tomcatServer1;
            #歡迎頁面,按照從左到右的順序查找頁面
            index index.jsp index.html index.htm;
        }
    }
​
    server {
        listen 80;
        server_name admin.web.itoken.funtl.com;
        location / {
            #域名 admin.web.itoken.funtl.com 的請求全部轉發到 tomcat_server2 即 tomcat2 服務上
            proxy_pass http://tomcatServer2;
            index index.jsp index.html index.htm;
        }
    }
} 
 
           
(3)啟動nginx在docker-compose.yml中配置:

      
version: '3'
  services:
    nexus:
      image: 'sonatype/nexus3'
      restart: always
      container_name: nexus
      ports:
        - '8081:8081'
      volumes:
        - '/usr/local/docker/nexus/data:/nexus-data'
    nignx:
      restart: always
      image: nginx
      container_name: nginx
      ports:
        - '8088:8088'
        - '9000:9000'
        - '80:80'
        - '/usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf'
        - '/usr/local/docker/nginx/wwwroot:/usr/share/nginx/wwwroot'         
​
           
# 定義負載均衡裝置的 Ip及裝置狀态 
upstream myServer {
    server 127.0.0.1:9090 down;
    server 127.0.0.1:8080 weight=2;
    server 127.0.0.1:6060;
    server 127.0.0.1:7070 backup;
}      
  • upstream

    :每個裝置的狀态:
  • down

    :表示目前的

    server

    暫時不參與負載
  • weight

    :預設為 1

    weight

    越大,負載的權重就越大。
  • max_fails

    :允許請求失敗的次數預設為 1 當超過最大次數時,傳回

    proxy_next_upstream

    子產品定義的錯誤
  • fail_timeout

    :

    max_fails

    次失敗後,暫停的時間。
  • backup

    :其它所有的非

    backup

    機器

    down

    或者忙的時候,請求

    backup

    機器。是以這台機器壓力會最輕
    代理伺服器?nginx是什麼?
代理伺服器?nginx是什麼?

4.實戰:

在一個虛拟主機配置兩個tomcat:
1.vim /etc/profile
    export CATALINA1_BASE="tomcat路徑"
    export CATALINA1_HOME="tomcat路徑"
    export Tomcat1Home=CATALINA1_BASE
    export CATALINA2_BASE="tomcat2路徑"
    export CATALINA2_HOME="tomcat2路徑"
    export Tomcat2Home=CATALINA2_BASE
2.在bin檔案中修改 catalina.sh
在首行加入:
    export CATALINA1_BASE=CATALINA1_BASE
    export CATALINA1_HOME=CATALINA1_HOME
3.修改host:
    8005→9005
    8009→9009
    8080→9080
    8443→9443
4. ./startup.sh啟動即可      

Nginx安裝

正常安裝( centos下):

1.先安裝gcc更新:

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

2.下載下傳PCRE安裝包:

wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

3.解壓安裝包:

[[email protected] src]# tar zxvf pcre-8.35.tar.gz       

4.編譯安裝:

[[email protected] pcre-8.35]# ./configure
[[email protected] pcre-8.35]# make && make install      

5.檢視pcre版本

[[email protected] pcre-8.35]# pcre-config --version      

6.下載下傳安裝nginx

wget http://nginx.org/download/nginx-1.10.2.tar.gz

7.進入nginx目錄編譯安裝

[[email protected] nginx-1.10.2]# ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
[[email protected] nginx-1.10.2]# make
[[email protected] nginx-1.10.2]# make install      

8.檢視版本:

[[email protected] nginx-1.10.2]# /usr/local/webserver/nginx/sbin/nginx -v      

這裡的配置使用的是webserver就是存放的檔案的路徑

關閉防火牆強:使其80端口開放:systemctl stop firewalld

通路:

正常安裝Ubuntu下:

1.先安裝gcc :

sudo apt-get update

2.安裝 依賴庫:

apt-get install zlib1g-dev

apt-get install openssl

3.下載下傳PCRE安裝包:

wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

4.解壓安裝包:

[[email protected] src]# tar zxvf pcre-8.35.tar.gz       

5.編譯安裝:

[[email protected] pcre-8.35]# ./configure
[[email protected] pcre-8.35]# make && make install      

6.檢視pcre版本

[[email protected] pcre-8.35]# pcre-config --version      

7.下載下傳安裝nginx

wget http://nginx.org/download/nginx-1.10.2.tar.gz

8.進入nginx目錄編譯安裝

[[email protected] nginx-1.10.2]# ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
[[email protected] nginx-1.10.2]# make
[[email protected] nginx-1.10.2]# make install      

9.檢視版本:

[[email protected] nginx-1.10.2]# /usr/local/webserver/nginx/sbin/nginx -v      

這裡的配置使用的是webserver就是存放的檔案的路徑

關閉防火牆強:使其80端口開放:systemctl stop firewalld

Docker安裝Nginx

docker pull nginx:latest

運作容器

docker run --name nginx-test -p 8080:80 -d nginx

  • --name nginx-test:容器名稱。
  • -p 8080:80: 端口進行映射,将本地 8080 端口映射到容器内部的 80 端口。
  • -d nginx: 設定容器在在背景一直運作。

DockerCompose安裝Nginx

version: '3.1'
  services:
  nginx:
    restart: always
    image: nginx
    container_name: nginx
    ports:
      - 81:80
    volumes:
      - ./conf/nginx.conf:/etc/nginx/nginx.conf
      - ./wwwroot:/usr/share/nginx/wwwroot
 # 配置nginx的資料卷:
  在dokcer檔案下建立:nginx的目錄下建立:nginx.conf檔案:
  在目前目錄建立wwwroot目錄
  這裡直接書寫為
  /usr/local/dokcer/nginx/conf/nginx.conf
  /usr/local/dokcer/nginx/wwwroot
  在nginx.conf目錄配置nginx的虛拟主機:      

運作:

docker-compose up

Nginx指令:

windows下:

cmd 進入Nginx解壓目錄 執行以下指令

start nginx

: 啟動nginx服務

nginx -s reload

:修改配置後重新加載生效

nginx -s reopen

:重新打開日志檔案

nginx -t -c /path/to/nginx.conf

測試nginx配置檔案是否正确

nginx -t

:驗證配置是否正确

nginx -V

:檢視Nginx的版本号

nginx -s stop

:快速停止或關閉Nginx

nginx -s quit

:正常停止或關閉Nginx

linux下:

啟動

./nginx

可跟後面的以下參數

​
-c </path/to/config> 為 Nginx 指定一個配置檔案,來代替預設的。路徑應為絕對路徑
​
-t 不運作,而僅僅測試配置檔案。nginx 将檢查配置檔案的文法的正确性,并嘗試打開配置檔案中所引用到的檔案。
​
-v 顯示 nginx 的版本。
​
-V 顯示 nginx 的版本,編譯器版本和配置參數。      

直接使用./nginx 進行啟動。

關閉:

ps -aux|grep nginx
kill -9 nginx主程序号      

設定開機自啟

1.進入

/lib/systemd/system

2.編輯nginx.service 檔案

vim nginx .service

[Unit]
Description=nginx service
After=network.target 
   
[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true 
   
[Install] 
WantedBy=multi-user.target      

3.加入開機自啟

systemctl enable nginx      

4.服務狀态:

# systemctl start nginx.service          啟動nginx服務
​
# systemctl stop nginx.service           停止服務
​
# systemctl restart nginx.service        重新啟動服務
​
# systemctl list-units --type=service     檢視所有已啟動的服務
​
# systemctl status nginx.service          檢視服務目前狀态
​
# systemctl enable nginx.service          設定開機自啟動
​
# systemctl disable nginx.service         停止開機自啟動      

Nginx配置:

########### 每個指令必須有分号結束。#################
#user administrator administrators;  #配置使用者或者組,預設為nobody nobody。
#worker_processes 2;  #允許生成的程序數,預設為1
#pid /nginx/pid/nginx.pid;   #指定nginx程序運作檔案存放位址
error_log log/error.log debug;  #制定日志路徑,級别。這個設定可以放入全局塊,http塊,server塊,級别以此為:debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #設定網路連接配接序列化,防止驚群現象發生,預設為on
    multi_accept on;  #設定一個程序是否同時接受多個網絡連接配接,預設為off
    #use epoll;      #事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大連接配接數,預設為512
}
http {
    include       mime.types;   #檔案擴充名與檔案類型映射表
    default_type  application/octet-stream; #預設檔案類型,預設為text/plain
    #access_log off; #取消服務日志    
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定義格式
    access_log log/access.log myFormat;  #combined為日志格式的預設值
    sendfile on;   #允許sendfile方式傳輸檔案,預設為off,可以在http塊,server塊,location塊。
    sendfile_max_chunk 100k;  #每個程序每次調用傳輸數量不能大于設定的值,預設為0,即不設上限。
    keepalive_timeout 65;  #連接配接逾時時間,預設為75s,可以在http,server,location塊。
​
    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #熱備
    }
    error_page 404 https://www.baidu.com; #錯誤頁
    server {
        keepalive_requests 120; #單連接配接請求上限次數。
        listen       4545;   #監聽端口
        server_name  127.0.0.1;   #監聽位址       
        location  ~*^.+$ {       #請求的url過濾,正則比對,~為區分大小寫,~*為不區分大小寫。
           #root path;  #根目錄
           #index vv.txt;  #設定預設頁
           proxy_pass  http://mysvr;  #請求轉向mysvr 定義的伺服器清單
           deny 127.0.0.1;  #拒絕的ip
           allow 172.18.5.54; #允許的ip           
        } 
    }
}      
  • 1、全局塊:配置影響nginx全局的指令。一般有運作nginx伺服器的使用者組,nginx程序pid存放路徑,日志存放路徑,配置檔案引入,允許生成worker process數等。
  • 2、events塊:配置影響nginx伺服器或與使用者的網絡連接配接。有每個程序的最大連接配接數,選取哪種事件驅動模型處理連接配接請求,是否允許同時接受多個網路連接配接,開啟多個網絡連接配接序列化等。
  • 3、http塊:可以嵌套多個server,配置代理,緩存,日志定義等絕大多數功能和第三方子產品的配置。如檔案引入,mime-type定義,日志自定義,是否使用sendfile傳輸檔案,連接配接逾時時間,單連接配接請求數等。
  • 4、server塊:配置虛拟主機的相關參數,一個http中可以有多個server。
  • 5、location塊:配置請求的路由,以及各種頁面的處理情況。

需要注意以下幾點:

1、幾個常見配置項:

  • 1.

    $remote_addr

    與 ​

    $http_x_forwarded_for

    用以記錄用戶端的ip位址;
  • 2.$remote_user :用來記錄用戶端使用者名稱;
  • 3.$time_local : 用來記錄通路時間與時區;
  • 4.$request : 用來記錄請求的url與http協定;
  • 5.$status : 用來記錄請求狀态;成功是200;
  • 6.$body_bytes_s ent :記錄發送給用戶端檔案主體内容大小;
  • 7.$http_referer :用來記錄從那個頁面連結通路過來的;
  • 8.$http_user_agent :記錄用戶端浏覽器的相關資訊;

2、驚群現象:一個網路連接配接到來,多個睡眠的程序被同時叫醒,但隻有一個程序能獲得連結,這樣會影響系統性能。

3、每個指令必須有分号結束。

Nginx載均衡機制

主要的算法有:

  1. weight輪訓(預設)

    :接收到的請求按照順序逐一配置設定到不同的後端伺服器,如果某個伺服器拓機的情況下,nginx會将其剔除隊列,請求受理情況不會受到影響,可以給不同的後端服務配置權重值,用于調整不同的伺服器上請求的配置設定率,權重越大被配置設定到的請求的幾率越大
  2. ip_hash

    :每個請求按照發起用戶端的ip的hash結果進行比對,這樣的算法下一個固定ip位址的用戶端總會通路到同一個後端伺服器,這也在一定程度上解決了叢集部署環境下session共享的問題
  3. fair

    :智能調整排程算法,動态的根據後端伺服器的請求處理到響應的時間進行均衡配置設定,響應時間短處理效率高的伺服器配置設定到請求的機率高,響應時間長處理效率低的伺服器配置設定到的請求少;結合了前兩者的優點的一種排程算法。但是需要注意的是nginx預設不支援fair算法,如果要使用這種排程算法,請安裝upstream_fair子產品
  4. url_hash

    :按照通路的url的hash結果配置設定請求,每個請求的url會指向後端固定的某個伺服器,可以在nginx作為靜态伺服器的情況下提高緩存效率。同樣要注意nginx預設不支援這種排程算法,要使用的話需要安裝nginx的hash軟體包
    代理伺服器?nginx是什麼?

繼續閱讀