Nginx

1. 什麼是nginx

Nginx是一款高性能的http 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器。由俄羅斯的程序設計師Igor Sysoev所開發,官方測試nginx能夠支支撐5萬併發鏈接,並且cpu、內存等資源消耗卻非常低,運行非常穩定。

 

 

2. 應用場景

1、http服務器。Nginx是一個http服務可以獨立提供http服務。可以做網頁靜態服務器

2、虛擬主機。可以實現在一臺服務器虛擬出多個網站。例如個人網站使用的虛擬主機。

3、反向代理,負載均衡。當網站的訪問量達到一定程度後,單臺服務器不能滿足用戶的請求時,需要用多臺服務器集羣可以使用nginx做反向代理。並且多臺服務器可以平均分擔負載,不會因爲某臺服務器負載高宕機而某臺服務器閒置的情況。

 

3. nginx安裝

下載nginx

 

官方網站:

http://nginx.org/

使用的版本是1.8.0版本。

 

Nginx提供的源碼。

3.1. 要求的安裝環境

1、需要安裝gcc的環境。yum install gcc-c++

2、第三方的開發包。

n PCRE

PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginxhttp模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫。

yum install -y pcre pcre-devel

注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。

n zlib

zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlibhttp包的內容進行gzip,所以需要在linux上安裝zlib庫。

yum install -y zlib zlib-devel

 

n openssl

OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。

nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。

yum install -y openssl openssl-devel

 

3.2. 安裝步驟

 

第一步:把nginx的源碼包上傳到linux系統

第二步:解壓縮

[root@localhost ~]# tar zxf nginx-1.8.0.tar.gz

第三步:使用configure命令創建一makeFile文件。

./configure \

--prefix=/usr/local/nginx \

--pid-path=/var/run/nginx/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/temp/nginx/client \

--http-proxy-temp-path=/var/temp/nginx/proxy \

--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \

--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \

--http-scgi-temp-path=/var/temp/nginx/scgi

注意:啓動nginx之前,上邊將臨時文件目錄指定爲/var/temp/nginx,需要在/var下創建tempnginx目錄

[root@localhost sbin]# mkdir /var/temp/nginx/client -p

第四步:make

第五步:make install

 

 

3.3. 啓動nginx

進入sbin目錄

[root@localhost sbin]# ./nginx

 

關閉nginx

[root@localhost sbin]# ./nginx -s stop

推薦使用:

[root@localhost sbin]# ./nginx -s quit

 

重啓nginx

1、先關閉後啓動。

2、刷新配置文件:

[root@localhost sbin]# ./nginx -s reload

 

3.4. 訪問nginx

 

默認是80端口。

注意:是否關閉防火牆。

 

4. 配置虛擬主機

就是在一臺服務器啓動多個網站。

如何區分不同的網站:

1、域名不同

2、端口不同

 

4.1. 通過端口區分不同虛擬機

Nginx的配置文件:

/usr/local/nginx/conf/nginx.conf

 

 

#user  nobody;

worker_processes  1;

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

 

events {

    worker_connections  1024;

}

 

 

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 {

        listen       80;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html;

            index  index.html index.htm;

        }

    }

}

 

可以配置多個server,配置了多個虛擬主機。

 

添加虛擬主機:

 

#user  nobody;

worker_processes  1;

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

 

events {

    worker_connections  1024;

}

 

 

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 {

        listen       80;

        server_name  localhost;(域名)

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        Location / {

            root   html;

            index  index.html index.htm;

        }

    }

    server {

        listen       81;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html-81;

            index  index.html index.htm;

        }

    }

}

 

重新加載配置文件

[root@localhost nginx]# sbin/nginx -s reload

 

 

4.2. 通過域名區分虛擬主機

4.2.1. 什麼是域名

域名就是網站。

www.baidu.com

www.taobao.com

www.jd.com

Tcp/ip

 

Dns服務器:把域名解析爲ip地址。保存的就是域名和ip的映射關係。

一級域名:

Baidu.com

Taobao.com

Jd.com

二級域名:

www.baidu.com

Image.baidu.com

Item.baidu.com

三級域名:

1.Image.baidu.com

Aaa.image.baidu.com

 

一個域名對應一個ip地址,一個ip地址可以被多個域名綁定。

 

本地測試可以修改hosts文件。

修改windowhosts文件:(C:\Windows\System32\drivers\etc

可以配置域名和ip的映射關係,如果hosts文件中配置了域名和ip的對應關係,不需要走dns服務器。

 

 

4.2.2. Nginx的配置

 

#user  nobody;

worker_processes  1;

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

 

events {

    worker_connections  1024;

}

 

 

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 {

        listen       80;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html;

            index  index.html index.htm;

        }

    }

    server {

        listen       81;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html-81;

            index  index.html index.htm;

        }

    }

    server {

        listen       80;

        server_name  www.taobao.com;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html-taobao;

            index  index.html index.htm;

        }

    }

    server {

        listen       80;

        server_name  www.baidu.com;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html-baidu;

            index  index.html index.htm;

        }

    }

}

 

 

域名的配置:

192.168.25.148 www.taobao.com

192.168.25.148 www.baidu.com


 

5. 反向代理

5.1. 什麼是反向代理

正向代理

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

反向代理:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

反向代理服務器決定哪臺服務器提供服務。

返回代理服務器不提供服務器。也是請求的轉發。

5.2. Nginx實現反向代理

兩個域名指向同一臺nginx服務器,用戶訪問不同的域名顯示不同的網頁內容。

兩個域名是www.sian.com.cnwww.sohu.com

nginx服務器使用虛擬機192.168.101.3 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

第一步:安裝兩個tomcat,分別運行在80808081端口。

第二步:啓動兩個tomcat

第三步:反向代理服務器的配置

複製一個server節點,root改成proxy_pass

配置upstream

upstream tomcat1 {

server 192.168.25.148:8080;

    }

    server {

        listen       80;

        server_name  www.sina.com.cn;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            proxy_pass   http://tomcat1;

            index  index.html index.htm;

        }

    }

    upstream tomcat2 {

server 192.168.25.148:8081;

    }

    server {

        listen       80;

        server_name  www.sohu.com;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            proxy_pass   http://tomcat2;

            index  index.html index.htm;

        }

    }

第四步:nginx重新加載配置文件

第五步:配置域名

hosts文件中添加域名和ip的映射關係

192.168.25.148 www.sina.com.cn

192.168.25.148 www.sohu.com

 

6. 負載均衡

如果一個服務由多條服務器提供,需要把負載分配到不同的服務器處理,需要負載均衡。

 upstream tomcat2 {

server 192.168.25.148:8081;

server 192.168.25.148:8082;

  }

 

可以根據服務器的實際情況調整服務器權重。權重越高分配的請求越多,權重越低,請求越少。默認是都是1

 upstream tomcat2 {

server 192.168.25.148:8081;

server 192.168.25.148:8082 weight=2;

    }

發佈了18 篇原創文章 · 獲贊 11 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章