nginx簡介及安裝步驟

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

一、應用場景

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

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

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

二、nginx安裝

1、下載nginx

官方網站:http://nginx.org/ 使用的版本是1.8.0版本。

2、要求安裝環境

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

2)第三方的開發包

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

yum install -y pcre pcre-devel

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

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

yum install -y zlib zlib-devel

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

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

yum install -y openssl openssl-deve

3、安裝步驟

第一步:把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

4、啓動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

6、配置虛擬主機

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

如何區分不同的網站:                 

  1. 域名不同
  2. 端口不同

通過端口區分不同虛擬機

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節點就是一個虛擬主機

 

 

    server {

        listen       80;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 
 

Html是nginx安裝目錄下的html目錄

 

 

        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

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章