日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Nginx部署及综合应用

發布時間:2024/1/17 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nginx部署及综合应用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

介紹

Nginx是一款輕量級的HTTP服務器軟件,由俄羅斯的Igor Sysoev開發。它能夠支持高達50000個并發連接數的響應,擁有強大的靜態資源處理能力,運行穩定,并且系統資源消耗非常低,現已逐漸被越來越多的用戶認可,目前很多大型網站都應用Nginx服務器作為后端網站程序的反向代理及負載均衡器,來提升整個站點的負載并發能力。

系統環境:

  • rhel 6.5 操作系統
  • 服務器IP地址:192.168.100.5
  • Nginx-1.6.0.tar.gz 百度下載 密碼:gz86

實現目標:

  • 安裝Nginx服務器
  • 啟用Nginx的狀態統計
  • 創建多虛擬主機
  • 實現用戶身份驗證訪問
  • 用戶授權訪問控制
  • 開始部署

    一. 安裝Nginx服務器

    1.安裝依賴包

    yum -y install pcre-devel zlib-devel gcc gcc-c++ make

    2.新建nginx管理用戶

    useradd -M -s /sbin/nologin nginx

    3.解壓

    tar xzvf nginx-1.6.0.tar.gz -C /opt

    4.配置

    cd /opt/nginx-1.6.0/

    ./configure \
    --prefix=/usr/local/nginx \
    --user=nginx \
    --group=nginx \
    --with-http_stub_status_module #開啟stub_status狀態統計模塊

    5.編譯及安裝

    make && make install

    6.建立nginx鏈接,便于系統管理

    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

    ########### nginx管理命令 ###########

    nginx -t #nginx配置文件檢查
    nginx #啟動
    killall -1 nginx #重啟nginx
    killall -3 nginx #停止nginx

    ######################################

    7.生成nginx管理腳本

    vi /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)
    ;;
    restart)
    $0 stop
    $0 start
    ;;
    reload)
    kill -s HUP $(cat $PIDF)
    ;;
    *)
    echo "Usage: $0 {start|stop|restart|reload}"
    exit 1
    esac
    exit 0

    8.nginx賦予執行權限

    chmod +x /etc/init.d/nginx

    9.nginx加入系統服務管理

    chkconfig --add nginx

    10.啟動nginx

    service nginx start

    11.測試


    二. 啟用Nginx的狀態統計

    1.進入nginx的conf目錄

    cd /usr/local/nginx/conf

    2.備份nginx.conf配置文件

    mv nginx.conf nginx.conf.bak

    3.過濾掉注釋文件至nginx.conf文件

    grep -v "#" nginx.conf.bak > nginx.conf

    4.編輯nginx.conf配置文件

    vim nginx.conf

    server {
    listen 80;
    server_name localhost;
    charset utf-8;

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

    #修改此處配置

    location ~ /status { #訪問位置為/status
    stub_status on; #打開狀態統計功能
    access_log off; #關閉此位置的日志記錄
    }

    error_page 500 502 503 504 /50x.html;

    location = /50x.html {

    root html;
    }
    }
    }

    5.檢測配置文件是否配置正確

    nginx -t

    6.重新啟動nginx

    service nginx restart

    7.測試


    三. 創建多虛擬主機

    1.編輯nginx主配置文件

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

    2.添加以下兩個虛擬主機

    server {
    listen 80;
    server_name www.web1.com; #域名
    location / {
    root /var/www/web1; #站點目錄
    index index.html index.php;
    }
    }

    server {
    listen 80;
    server_name www.web2.com; #域名
    location / {
    root /var/www/web2; #站點目錄
    index index.html index.php;
    }
    }

    3.檢測配置文件是否配置正確

    nginx -t

    4.重新啟動nginx

    service nginx restart

    5.測試

    訪問www.web1.com 站點:

    訪問www.web2.com 站點:

    四. 實現用戶身份驗證訪問

    1.使用htpasswd生成用戶認證文件,需要輸入兩次密碼確認

    htpasswd -c /usr/local/nginx/passwd.db zhangsan

    2.修改密碼文件的權限

    chmod 400 /usr/local/nginx/passwd.db

    3.修改密碼文件所有者

    chown nginx /usr/local/nginx/passwd.db

    4.編輯nginx主配置文件

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

    location / {
    auth_basic "secret"; #添加認證配置
    auth_basic_user_file /usr/local/nginx/passwd.db; #指定密碼文件路徑
    root html;
    index index.html index.htm;
    }

    5.檢測配置文件是否配置正確

    nginx -t

    6.重新啟動nginx

    service nginx restart

    7.測試


    五. 用戶授權訪問控制

    1.編輯nginx主配置文件

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

    location / {
    deny 192.168.100.30; #禁止192.168.100.30訪問
    allow all; #允許其他主機訪問
    root html;
    index index.html index.htm;
    }

    2.檢測配置文件是否配置正確

    nginx -t

    3.重新啟動nginx

    service nginx restart

    4.測試

    未禁止訪問時,該客戶機可以正常訪問瀏覽

    添加禁止訪問設置后,該客戶機已經不能夠訪問瀏覽了

    轉載于:https://blog.51cto.com/10316297/2130972

    總結

    以上是生活随笔為你收集整理的Nginx部署及综合应用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。