nginx入門

一、安裝nginx

1、下載nginx

http://nginx.org/download/nginx-1.8.1.tar.gz
上傳到Linux服務器,我這里是上傳到/usr/tool/目錄下
進入tool文件夾下,解壓上傳的nginx壓縮包

[root@localhost /]# cd /usr/tool/
[root@localhost tool]# tar -zxvf nginx-1.8.1.tar.gz

2、準備環境

需要安裝gcc環境和相關第三方開發包,執行如下命令

[root@localhost tool]#yum -y install gcc-c++
[root@localhost tool]#yum install -y pcre pcre-devel
[root@localhost tool]#yum install -y zlib zlib-devel
[root@localhost tool]#yum install -y openssl openssl-devel

命令解釋
yum -y install gcc-c++
需要安裝gcc的環境。

yum install -y pcre pcre-devel
PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫。
注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。

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

yum install -y openssl openssl-devel
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,并提供豐富的應用程序供測試或其它目的使用。
nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。

3、創建目錄

需要在/var下創建temp及nginx目錄

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

4、Nginx編譯

[root@localhost /]# cd /usr/tool/nginx-1.8.1/
[root@localhost nginx-1.8.1]# ./configure
--prefix=/usr/tool/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

5、安裝Nginx:

安裝命令:make & make install

[root@localhost nginx-1.8.1]# make & make install

進入nginx目錄,查看目錄下有sbin文件夾,安裝成功

[root@localhost nginx-1.8.1]# cd /usr/tool/nginx
[root@localhost nginx]# ll

6、啟動nginx

進入sbin目錄

[root@localhost sbin]# ./nginx

7、訪問nginx

輸入安裝nginx的IP,默認端口為80

http://192.168.65.129/


image.png

如看到上圖信息,nginx安裝與啟動成功

二、配置虛擬主機

nginx的配置文件在
/usr/tool/nginx-1.8.1/conf/nginx.conf

1、通過端口區分

1.1、配置文件修改如下

每個server監聽1個端口
修改完成需要每次配置文件修改之后都需要重新加載配置文件,命令如下

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

#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   html81;
            index  index.html index.htm;
        }
    }
}

1.2、復制文件夾用于81端口使用

[root@localhost nginx]# cp html html81 -r

修改html81目錄下index.html內容來區分端口

2、通過域名區分

2.1、修改nginx的配置文件如下

修改完刷新文件

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


#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   html81;
            index  index.html index.htm;
        }

    }

    server {
        listen       80;
        server_name  www.test01.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   htmltest01;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  www.test02.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   htmltest02;
            index  index.html index.htm;
        }
    }
}

2.2、復制文件夾

復制文件夾后并修改里面的index.html文件夾做區分

[root@localhost nginx]# cp html htmltest01 -r
[root@localhost nginx]# cp html htmltest02 -r

2.3、修改本機hosts

windows的hosts的文件在
C:\Windows\System32\drivers\etc
增加內容如下,ip換成安裝nginx機器的地址

192.168.65.129 www.test01.com
192.168.65.129 www.test02.com

2.4、瀏覽器訪問

瀏覽器輸入
http://www.test01.com/

image.png

瀏覽器輸入
http://www.test02.com/

image.png

至此,通過端口和域名區分虛擬主機設置完成

三、nginx配置反向代理

1、安裝2個Tomcat

我的Linux已經有1個Tomcat,復制2兩份
分別修改端口為8080和80801
分別修改Tomcat下webapp目錄下ROOT目錄下的index.jsp以做區別
然后分別啟動2個Tomcat

[root@localhost tomcat]# cp tomcat8 tomcat8-8080 -r
[root@localhost tomcat]# cp tomcat8 tomcat8-8081 -r

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;

   upstream tomcat8080 {
    server 192.168.65.129:8080;
    }
    server {
        listen       80;
        server_name  www.test01.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcat8080;
            index  index.html index.htm;
        }
    }
    upstream tomcat8081 {
    server 192.168.65.129:8081;
    }
    server {
        listen       80;
        server_name  www.test02.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcat8081;
            index  index.html index.htm;
        }
    }
}

3、瀏覽器訪問

http://www.test01.com/

image.png

http://www.test02.com/

image.png

反向代理配置成功

四、nginx配置負載均衡

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;

   upstream tomcat8080 {
    server 192.168.65.129:8080;
    server 192.168.65.129:8081;
    }
    server {
        listen       80;
        server_name  www.test01.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcat8080;
            index  index.html index.htm;
        }
    }

}

第一次訪問http://www.test01.com/

image.png

第二次訪問http://www.test01.com/

image.png

至此,nginx的負載均衡配置完成

負載均衡的幾種常用方式

1、輪詢(默認)

每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。

upstream tomcat8080 {
    server 192.168.65.129:8080;
    server 192.168.65.129:8081;
}

2、weight

指定輪詢幾率,weight和訪問比率成正比,用于后端服務器性能不均的 情況。

upstream tomcat8080 {
    server 192.168.65.129:8080 weight=3;
    server 192.168.65.129:8081 weight=7;
}

權重越高,在被訪問的概率越大,如上例,分別是30%,70%。

3、ip_hash指令

上述方式存在一個問題就是說,在負載均衡系統中,假如用戶在某臺服務器上登錄了,那么該用戶第二次請求的時候,因為我們是負載均衡系統,每次請求都會重新定位到服務器集群中的某一個,那么已經登錄某一個服務器的用戶再重新定位到另一個服務器,其登錄信息將會丟失,這樣顯然是不妥的。

我們可以采用ip_hash指令解決這個問題,如果客戶已經訪問了某個服務器,當用戶再次訪問時,會將該請求通過哈希算法,自動定位到該服務器。

每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。

upstream tomcat8080 {
    ip_hash;
    server 192.168.65.129:8080;
    server 192.168.65.129:8081;
}

4、fair(第三方)

按后端服務器的響應時間來分配請求,響應時間短的優先分配。

upstream tomcat8080 {
    server 192.168.65.129:8080;
    server 192.168.65.129:8081;;
    fair;
}

5、url_hash(第三方)

按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。

upstream tomcat8080 {
    server squid1:3128;
    server squid2:3128;
    hash $request_uri;
    hash_method crc32;
}

每個設備的狀態設置為:
1.down 表示單前的server暫時不參與負載
2.weight 默認為1.weight越大,負載的權重就越大。
3.max_fails:允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream模塊定義的錯誤
4.fail_timeout:max_fails次失敗后,暫停的時間。
5.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。

配置實例:

#user  nobody;
worker_processes  4;
events {
    # 最大并發數
    worker_connections  1024;
}
http{
    # 待選服務器列表
    upstream tomcat8080 {
        # ip_hash指令,將同一用戶引入同一服務器。
        ip_hash;
        server 192.168.65.129:8080 fail_timeout=60s;
        server 192.168.65.129:8081;
        }

    server{
                # 監聽端口
                listen 80;
                # 根目錄下
                location / {
                    # 選擇哪個服務器列表
                    proxy_pass http://tomcat8080 ;
                }

            }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,791評論 6 545
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,795評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,943評論 0 384
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 64,057評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,773評論 6 414
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,106評論 1 330
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,082評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,282評論 0 291
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,793評論 1 338
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,507評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,741評論 1 375
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,220評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,929評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,325評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,661評論 1 296
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,482評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,702評論 2 380

推薦閱讀更多精彩內容