Nignx服務基礎

一、Nginx簡介;

概述:Nginx是一款由俄羅斯開發的開源的高性能HTTP服務器和反向代理服務器,同時支持IMAP/POP3/SMTP代理服務,其性能優勢着爲顯著,官網上稱:單臺nginx服務器可以處理50000併發;
特點:高性能、穩定、消耗硬件資源小、能夠處理大併發,主要用於靜態的解析,動靜頁面的分離;
優勢:
1.作爲Web服務器,nginx處理靜態文件、索引文件以及自動索引效率非常高。
2.作爲代理服務器,Nginx可以實現無緩存的反向代理加速,提高網站運行速度。
3.作爲負載均衡服務器,Nginx既可以在內部直接支持Rails和PHP,也可以支持HTTP代理服務器,對外進行服務。同時支持簡單的容錯和利用算法進行負載均衡。

二.安裝nginx程序

(1)安裝支持軟件
Nginx的配置以及運行需要pcre、zlib等軟件包的支持。
yum install gcc gcc-c++ pcre-devel zlib-devel
(2)創建運行用戶組
Nginx服務程序默認以nobody身份運行,建議創建專門的用戶賬號,以便更準確控制器訪問權限,怎加靈活性,安全、降低風險。
useradd -M -s /sbin/nologin nginx
(3)編譯安裝Nginx

root@localhost opt]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]#./configure \
--prefix=/usr/local/nginx \  #安裝目錄
 --user=nginx \                                  #運行用戶爲Nginx
 --group=nginx \                             #運行組爲Nginx
 --with-http_stub_status_module     #模塊以支持狀態統計,便於查看服務器的信息連接
 [root@localhost nginx-1.12.0]# make && make install

(4)優化路徑
爲了方便管理,可以爲主程序ngin創建鏈接文件,方便直接用nginx命令直接調用nginx主程序

ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
(5)檢查配置

[root@localhost ~]# 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

(6)啓動和停止Nginx

[root@localhost ~]# nginx  啓動
[root@localhost ~]# killall -s HUP nginx  #重載配置
[root@localhost ~]# killall -s QUIT nginx #停止服務

(6)使用Nginx服務腳本
爲了使用Nginx服務啓動、停止、重載等操作更加方便,並使用chkconfig和service工具來進行管理。

[root@localhost ~]# vim /etc/init.d/nginx 
#!/bin/bash
#chkconfig: - 99 20
 description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
        $PROG
;;
stop)
        kill -s QUIT $(cat $PIDF)
;;
reload)
        $0 stop
        $0 start
;;
restart)
        kill -s HUP $(cat $PIDF)
;;
*)
        echo "Usage:$0 {start|stop|restart|reload}"
;;
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx

三.配置文件nginx.conf

1.全局配置

#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; #PID文件的位置

2.I/O事件配置
使用“event{}”定界表記,用來指定Nginx的進程的I/O響應模型,每個進程的連接數設置。

events {
    worker_connections  1024;   #每個進程處理4096個連接
}

3.HTTP配置
使用“http{}”界定標記包括訪問日誌,HTTP端口、頁面目錄、默認字符集、連接保持、以及虛擬Web主機、PHP解析等一系列設置。其中大部分配置語句包含在界定標記“server{}”內

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;#保持連接超時
        #access_log  logs/access.log  main;

    #gzip  on;

    server { #Web服務的監聽配置
        listen 192.168.242.220:80;#監聽地址端口
        server_name  192.168.242.220:80;#網站名稱
        charset utf-8;#網頁的默認字符集
        access_log  logs/www.kgc.com.host.access.log;#訪問日誌位置

        location / {  #根目錄配置
            root /var/www/html/kgc; #網站跟目錄的位置
            index  index.html index.htm;#默認網頁(索引頁)
        }

        error_page   500 502 503 504  /50x.html; #內部錯誤的反饋頁面
        location = /50x.html {   #錯誤頁面配置
            root   html;
        }
    }

3.訪問狀態統計
Nginx內置了HTTP_STUB_STATUS狀態統計模塊,用來反饋當前的Web訪問情況,配置編譯數時可以怎加--with-http_stub_status_mdule來開啓此模塊支持

要使用nginx的狀態統計功能,除了啓用內建模塊以外,還需要修改nginx.conf配置文件。指定訪問位置並添加stub_status

location / {
            stub_status on;#打開狀態統計功能
            access_log off;#關閉此位置的日誌記錄
            root /var/www/html/kgc;
            index  index.html index.htm;
        }

Nginx訪問控制

基於授權的訪問控制步驟
1.使用htpasswd生成用戶認證文件,如果沒有該命令,可使用yun安裝httpd-tools軟件包


[root@localhost ~]#htpasswd /usr/local/nginx/passwd.db jiji #在/usr/local/nginx目錄生成passwd.db文件,用戶名爲jiji,密碼輸入兩次。在passwd.db中生成用戶和密碼的密文

2.修改密碼文件的權限爲400,將所有者改爲nginx

[root@localhost ~]# chmod 400 /usr/local/nginx/passwd.db 
[root@localhost ~]# chown nginx /usr/local/nginx/passwd.db

3.修改主配置文件nginx.conf 添加相應認證配置項

location / {
            stub_status on;#打開狀態統計功能
            access_log off;#關閉此位置的日誌記錄
            root /var/www/html/kgc;
            index  index.html index.htm;
            auth_basic "secret";#添加認證配置
            auth_basci_user_file /usr/loacl/nginx/passwd.db
        }

基於客戶端的訪問控制
1.deny IP /IP段 :拒絕某個IP或IP段的客戶端
2.allow IP/IP段: 允許某個IP或IP段的客戶端
3.規則從上往下執行,如匹配則停止,不再往下匹配


location / {
            stub_status on;
            access_log off;
            root /var/www/html/kgc;
            index  index.html index.htm;

             deny 192.169.10.10; #客戶端IP
             allow all;
        }

Nginx虛擬主機

每個虛擬Web站點擁有獨立的server{}配置端,各自監聽的ip地址 端口號可以單獨指定,網絡名稱不同
1.基於IP的虛擬主機
2.基於域名的虛擬主機
3.基於端口的虛擬主機

基於域名虛擬主機設置

server { #加入www.kgc.com對應的站點
        listen       80;  
        server_name  www.kgc.com; #監聽地址
        charset utf-8;
        access_log  logs/www.kgc.com.host.access.log;

        location / {
            root /var/www/html/kgc; #www.kgc.com工作目錄
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

server { #加入www.accp.com 對應的站點
        listen       80; 
        server_name  www.accp.com;#監聽地址
        charset utf-8;
        access_log  logs/www.accp.com.host.access.log;

        location / {
            root /var/www/html/accp; #工作目錄
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        }

基於ip地址

server {
        listen 192.168.242.220:80; #監聽192.168.242.220
        server_name  192.168.242.220:80; 
        charset utf-8;
        access_log  logs/www.kgc.com.host.access.log;

        location / {
            root /var/www/html/kgc;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

server {
        listen 192.168.242.167:80;
        server_name  192.168.242.167:80; #監聽192.168.242.167
        charset utf-8;
        access_log  logs/www.accp.com.host.access.log;

        location / {
            root /var/www/html/accp;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        }

基於端口

server {
        listen 192.168.242.220:80;
        server_name  192.168.242.220:80;
        charset utf-8;
        access_log  logs/www.kgc.com.host.access.log;

        location / {
            root /var/www/html/kgc;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen 192.168.242.220:8080;
        server_name  192.168.242.220:8080;
        charset utf-8;
        access_log  logs/www.accp.com.host.access.log;

        location / {
            root /var/www/html/accp;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章