nginx服務

    Nginx是一個高性能的http和反向代理服務器,同時也是一個imap/pop3/smtp代理服務器。十分輕量級的http服務。

常用的web服務器

    UNIX和LINUX平臺下

    Apache  nginx    lighttpd  Tomcat(JAVA語言編寫的)  IBMwebsphere

    Windows平臺下

    Intenet   information  server  (IIS)

部署nginx服務器

一、 nginx 安裝與配置

    源碼包包安裝 所以必須安裝開發庫軟件包組合開發工具軟件包組。

[root@localhost ~]# yum groupinstall "開發工具" " 開發庫" -y

    需要從網上下載軟件包。RPM  源碼包。

    Nginx可以在服務運行的時候升級。平滑升級。

[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tail -1 /etc/passwd
[root@localhost nginx-package]# tar zxvf nginx-0.8.55.tar.gz
[root@localhost nginx-0.8.55]# ./configure --help
[root@localhost ~]# rpm -qa | grep ssl
[root@localhost nginx-0.8.55]# ./configure 
指定安裝路徑--prefix=/usr/local/nginx 
指定用戶--user=nginx 
指定組--group=nginx 
支持狀態查看--with-http_stub_status_module 
支持https--with-http_ssl_module

Nginx  rewrite   地址重寫  支持修改用戶訪問的目標地址

用perl+正則表達式的方式匹配目標地址

[root@localhost ~]# yum install pcre-devel –y
[root@localhost ~]# rpm -qa | grep -i PCRE
pcre-devel-6.6-6.el5_6.1
pcre-6.6-6.el5_6.1
pcre-6.6-6.el5_6.1
pcre-devel-6.6-6.el5_6.1
[root@localhost nginx-0.8.55]# make
[root@localhost nginx-0.8.55]#make install
[root@localhost nginx]# ls
Conf  存放配置文件  主配置文件nginx.conf
html  網頁目錄
logs  日誌文件
sbin  可執行命令   啓動腳本

啓動nginx服務  默認監聽80端口,要把其他網站服務停掉

[root@localhost conf]# netstat -antup | grep :80
tcp        0      0 :::80                       :::*                        LISTEN      4087/httpd   [root@localhost conf]# /etc/init.d/httpd stop
[root@localhost sbin]# ./nginx –h
-?,-h  : this help    查看幫助信息
-v    : show version and exit    查看版本號
-V   : show version and configure options then exit 查看編譯的配置信息
-t    : test configuration and exit  測試配置文件nginx.conf有無錯誤
-q   : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix     : set prefix path (default: /usr/local/nginx/)
-c  filename  : set configuration file (default: conf/nginx.conf)使用指定的配置文件
-g directives : set global directives out of configuration file
[root@localhost sbin]# ./nginx
[root@localhost sbin]# netstat -antup | grep :80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      13312/nginx   [root@localhost sbin]# ps aux | grep nginx
傳輸協議TCP,端口號80,所有者nginx
[root@localhost sbin]# elinks --dump http://localhost
Welcome to nginx!

停止nginx服務

沒有提供停止服務的腳本,如果要停止服務殺進程。

Pkill  -信號  進程名
Kill  -信號   pid號

信號:

TREM或INT    快速關閉

QUIT   從容關閉,關閉主進程順便關閉工作子進程

HUP  重載配置用新的配置開始新的工作進程從容關閉舊的工作進程

USR1  重新打開日誌文件

USR2  平滑升級可執行程序

WINCH  從容關閉工作進程  不會立即關閉子進程

[root@localhost sbin]# pkill -HUP nginx
平滑升級nginx軟件
[root@localhost nginx-1.0.5]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
[root@localhost sbin]# mv nginx nginx-old
[root@localhost objs]# mv nginx /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.0.5]# make upgrade 平滑升級
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

二、 nginx虛擬主機

基於IP地址虛擬主機(通過IP地址區分用戶訪問)

基於端口虛擬主機(通過端口區分用戶訪問)

基於域名虛擬主機(通過主機名區分用戶訪問)

Vim  安裝目錄/conf/nginx.conf
http {
server{
location/{
}
}
Server{
}
}
[root@localhost conf]# grep -vE "^$|#" nginx.conf
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  localhost;   
        location / {
            root   html;   網頁根目錄
            index  index.html index.htm;    首頁文件名
        }
        error_page   500 502 503 504  /50x.html;  錯誤顯示頁面
        location = /50x.html {
            root   html;
        }
    }
}
[root@localhost conf]# grep -vE "^$|#" nginx.conf > nginx-1.conf
[root@localhost conf]# vim nginx-1.conf
[root@localhost nginx]# pkill -9 nginx
[root@localhost nginx]# sbin/nginx -c conf/nginx-1.conf 
[root@localhost nginx]# netstat -antup | grep :80

基於端口的虛擬主機

[root@localhost ~]# mkdir /web8000
[root@localhost ~]# mkdir /web8090
[root@localhost ~]# echo web8000 > /web8000/index.html
[root@localhost ~]# echo web8090 > /web8090/index.html
[root@localhost nginx]# vim conf/nginx1.conf
server {
        listen  8000;
        location / {
                root /web8000;
                index index.html;
        }
                }
    server {
        listen  8090;
        location / {
                root /web8090;
                index index.html;
        }
                }
[root@localhost nginx]# sbin/nginx -c conf/nginx1.conf
[root@localhost ~]# elinks --dump http://localhost:8000
   web8000
[root@localhost ~]# elinks --dump http://localhost:8090
   web8090

基於IP地址的虛擬主機(通過IP地址區分用戶訪問)

生產環境中,要有對應IP地址的物理網卡。

[root@localhost ~]# ifconfig eth0:0 192.168.1.100
[root@localhost ~]# ifconfig eth0:1 192.168.1.200
[root@localhost ~]# echo web100 > /web8000/index.html
[root@localhost ~]# echo web200 > /web8090/index.html
[root@localhost nginx]# vim conf/nginx1.conf
server {
        listen  192.168.1.100:80;
        location / {
                root /web8000;
                index index.html;
        }
                }
    server {
        listen  192.168.1.200:80;
        location / {
                root /web8090;
                index index.html;
        }
                }
[root@localhost nginx]# pkill -9 nginx
[root@localhost nginx]# netstat -antup | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      17323/nginx    
[root@localhost ~]# elinks --dump http://192.168.1.100
   web100
[root@localhost ~]# elinks --dump http://192.168.1.200
   web200

基於域名的虛擬主機(通過主機名區分用戶的訪問)

[root@localhost nginx]# vim conf/nginx1.conf
server {
        listen  80;
        server_name www.example.com
        location / {
                root /web8000;
                index index.html;
        }
                }
    server {
        listen  80;
        server_name bbs.example.com
        location / {
                root /web8090;
                index index.html;
        }
                }
[root@localhost ~]# elinks --dump http://bbs.example.com
   web200
[root@localhost ~]# elinks --dump http://www.example.com
   web100
[root@localhost nginx]# pkill -9 nginx
[root@localhost nginx]# sbin/nginx -c conf/nginx1.conf

用戶訪問控制

//只允許192.168.1.1的訪問(小範圍的寫在上面)

Location /{
Allow192.168.1.1
Allow192.168.1.0/24
Denyall
}

拒絕訪問(小範圍的寫在上面)

Location /{
deny192.168.1.1
deny192.168.1.0/24
allowall
}

用戶認證(用戶在訪問頁面的時候呀輸入正確的用戶名密碼才能訪問)

Location / {
Auth_basic“auth-domain”;
Auth_basic_user_file/usr/local/nginx/conf/authuser.txt
}

生成用戶名密碼

[root@www ~]# htpasswd -c /usr/local/nginx/conf/authuser.txt admin

    -c  Create a new file.  指定用戶名密碼存放的文件

Nginx反向代理(應用層的負載均衡集羣)

[root@www nginx]# vim conf/nginx.conf
http {
    upstream ly {
        server 192.168.1.254:80;
        server 192.168.1.2:80;
}
server {
location / {
            proxy_pass http://ly;
}
}
}

Nginx的反向代理優點:

可以自動的對後端的網站服務器進行健康檢查。若後端某一臺服務器不能提供網站服務時,不會將請求發給這臺服務器。

分發時候使用的算法:使用最多爲輪詢和權重值。

輪詢算法:(默認,權重值時1)

將請求平均的分發給服務器組服務器。

Weight  指定輪詢機率。權重和訪問比率成正比。通常用於後端服務器性能不同的情況,默認值爲1.

upstream ly {
        server 192.168.1.254:80 weight=3;
        server 192.168.1.2:80 weight=2;
}

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

upstream ly {
  ip_hash;
        server 192.168.1.254:80;
        server 192.168.1.2:80;
}

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

不是默認支持。

upstream ly {
  fair;
        server 192.168.1.254:80;
        server 192.168.1.2:80;
}

服務器主機狀態類型

Down  表示當前server暫時不參與負載。

Backup: 當其他所有的非backup機器down或者忙的時候,請求會發給backup機器。這臺機器壓力最輕。

Max_fails: 允許請求失敗的次數。默認爲1.當超過此次數時,返回proxy_next_upstream模塊定義的錯誤。

Fail_timeout:max_fails次失敗後,暫停提供服務的時間。

Nginx的優點。地址重寫。防盜鏈。Nginx做反向代理時能否緩存訪問用戶的請求。


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