企業中常用的Nginx網站服務相關配置

前言:

在各種網站服務器軟件中,除了 Apache HTTP Server 外,還有一款輕量級的HTTP 服務器軟件------Nginx,由俄羅斯的 Igor Sysoev 開發,其穩定、高效的特性逐漸被越來越多的用戶認可。

一、Nginx概述

  • 一款高性能、輕量級Web服務軟件

  • 穩定性高

  • 系統資源消耗低

  • 對HTTP併發連接的處理能力高

  • 單臺物理服務器可支持30000 ~ 50000個併發請求

  • 佔用內存少,併發能力強

二、編譯安裝Nginx 服務

1、關閉防火牆,將安裝 ngnix 所需的軟件包上傳到 /opt 目錄下

systemctl stop firewalld.service 
systemctl disable firewalld.service 
setenforce 0

在這裏插入圖片描述
2、安裝依賴包
(nginx 的配置及運行需要 pcre zlib 等軟件包的支持,因此需要安裝這些安裝的開發包,以便提供相應的庫和頭文件)

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

3、創建運行用戶、組(Nginx 服務程序默認以 noboday 身份運行,建議爲其創建專門的用戶賬號,以便更準確的控制其訪問權限)

useradd -M -s /sbin/nologin/ nginx	 #-M 代表不創建家目錄

4、編譯安裝 Nginx

tar zxvf nginx-1.12.2.tar.gz -C /opt/

cd /opt/nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

#每行代表的意思
./configure \
--prefix=/usr/local/nginx \      #指定nginx的安裝路徑
--user=nginx \        #指定用戶名
--group=nginx \      #指定組名
--with-http_stub_status_module	  #啓用 http_stub_ status_ module 模塊以支持狀態統計

make && make install

ln -s /usr/local/nginx/sbin/* /usr/local/sbin/     #創建軟鏈接便於系統管理,讓系統識別nginx的操作命令

5、檢查、啓用、重啓、停止 nginx服務

nginx -t     #檢查配置文件是否配置正確

nginx     #啓動

----停止nginx 服務------
cat /usr/local/nginx/logs/nginx.pid    #首先查看nginx的PID號
kill -3  <PID號>
kill -s QUIT <PID號>
killall -3 nginx
killall -s QUIT nginx


----重載------
kill -1 <PID號>
kill -s HUP <PID號>
killall -1 nginx
killall -s HUP <PID號>

----日誌分隔,重新打開日誌文件-------
Kill -USR1 <PID號>

-----平滑升級------
kill -USR2 <PID號>

6、添加 Nginx 系統服務
方法一:

vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
   $COM
   ;;
stop)
   kill -s QUIT $(cat $PID)
   ;;
restart)
   $0 stop
   $0 start
   ;;
reload)
   kill -s HUP $(cat $PID)
   ;;
*)
       echo "Usage: $0 {start|stop|restart|reload}"
       exit 1
esac
exit 0
chmod +x /etc/init.d/nginx
chkconfig --add nginx         #添加爲系統服務
systemctl stop nginx
systemctl start nginx

方法二:

vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
---------------------------------------------------------
#每行意義
[Unit]
Description=nginx		#描述
After=network.target	#描述服務類別
[Service]
Type=forking			#後臺運行形勢
PIDFile =/usr/local/nginx/logs/nginx.pid	#PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx		#啓動服務
ExecrReload=/bin/kill -s HUP $MAINPID	#根據PID重載配置
ExecrStop=/bin/kill -s QUIT $MAINPID		#根據PID終止進程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service		#設置754權限是一種安全優化
systemctl start nginx.service
systemctl enable nginx.service

在這裏插入圖片描述

三、認識 Nginx 服務的主配置文件 nginx.conf

cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf

1、全局配置

#user nobody;					#運行用戶,若編譯時未指定則默認爲 nobody
worker_ processes 1;			#工作運行數量,可配置成服務器內核數*2,如果網站訪問量不大,一般設爲1就夠用了
#error_log logs/error.log;		#錯誤日誌文件的位置
#pid logs/nginx.pid;			#PID文件的位置

2、I/O事件配置

events {
   
   
  use epoll;              #使用 epoll 模型,2.6及以上版本的系統內核,建議使用 epoll 模型以提高性能
  worker_connections 4096;    #每個進程處理 4096 個連接
}

#如提高每個進程的連接數還需執行“ulimit -n 65535”命令臨時修改本地每個進程可以同時打開的最大文件數。
#在Linux 平臺上,在進行高併發TCP連接處理時,最高的併發數量都要受到系統對用戶單一進程同時可打開文件數量的限制(這是因爲每個TCP連接都要創建一個socket句柄,每個socket句柄同時也是一個文件句柄)。
#可使用ulimit -a 命令查看系統允許當前用戶進程打開的文件數限制。

3、HTTP 配置

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;
    ##此項允許或禁止使用socket的TCP_CORK的選項(發送數據包前先緩存數據),此選項僅在使用sendfile的時候使用
    #tcp_nopush     on;
    ##連接保持超時時間,單位是秒
    #keepalive_timeout  0;
    keepalive_timeout  65;              #連接保持超時
    ##gzip模塊設置,設置是否開啓gzip壓縮輸出
    #gzip  on;
##web 服務的監聽配置
    server {
   
    
        listen       80;               #監聽地址及端口
        server_name  www.gcc.com;      #站點域名,可以有多個,用空格隔開
        #charset koi8-r;        #網頁的默認字符集  
        location / {
   
              #根目錄配置
            root   html;       #網站根目錄的位置/usr/local/nginx/html
            index  index.html index.htm;   #設置首頁文件名
        }
        ##內部錯誤的反饋頁面
        error_page  500 502 503 504 /50x.html;
        ##錯誤頁面配置
        location = /50x.html {
   
   
             root   html;
        }
      }
  }

日誌格式設定:

$remote_addr  $http_x_forwarded for用以記錄客戶端的ip地址;
sremote_user:用來記錄客戶端用盧名稱;
stime_local: 用來記錄訪問時間與時區;
$request: 用來記錄請求的url與http協議;
$status: 用來記錄請求狀態;成功是200,
$body_bytes_sent:記錄發送給客戶端文件主體內容大小;
$http_referer:用來記錄從哪個頁面鏈接訪問過來的;
$http_user_agent:記錄客戶瀏覽器的相關信息;通常web服務器放在反向代理的後面,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理服務器的iP地址。反向代理服務器在轉發請求的http頭信息中,可以增加x_forwarded_for信息,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務器地址。

location常見配置指令, root, alias, proxy_pass
root (根路徑配置) :請求www.gcc.com/test/1.jpg,會返回文件/usr/local/nginx/html/test/1.jpg
alias (別名配置) :請求www.gcc.com/test/1.jpg,會返回文件/usr/local/nginx/html/1.jpg

proxypass (反向代理配置)
proxy_pass http://127.0.0.1:8080/;  會轉發請求到http://127.0.0.1:8080/1.jpg
proxy_ pass http://127.0.0.1:8080;  會轉發請求到http://127.0.0.1:8080/test/1.jpg

設置臨時域名解析

echo "192.168.200.50 www.gcc.com" >> /etc/hosts

在這裏插入圖片描述


cd /usr/local/nginx/html
mkdir test
[root@localhost html]#cd test/
[root@localhost test]#vim index.html
   this is test!

systemctl restart nginx.service

在這裏插入圖片描述

四、訪問狀態統計配置

1、先使用命令 /usr/local/nginx/sbin/nginx -V 查看已安裝的Nginx 是否包含 HTTP_STUB_STATUS 模塊
2、修改 nginx.conf 配置文件,指定訪問位置並添加 stub_status 配置

cd /usr/local/nginx/conf/
cp nginx.conf nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
......
http {
   
     
......
       server {
   
     
        listen       80;
        server_name  www.gcc.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
   
     
            root   html;
            index  index.html index.htm;
        }
 ##添加 stub_status 配置
        location /status {
   
            #訪問位置爲/status
            stub_status on;     #打開狀態統計功能
            access_log off;     #關閉此位置的日誌記錄
        }

3、重啓服務,訪問測試

systemctl restart nginx.service 

4、瀏覽器訪問 http://192.168.200.50/status
Active connections : 表示當前的活動連接數;
service accepts handled requests:表示已經處理的連接信息,三個數字依次表示已處理的連接數、成功的TCP 握手次數、已處理的請求數。

在這裏插入圖片描述

五、基於授權的訪問控制

1、生成用戶密碼認證文件

yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db zhangsan
chown nginx /usr/local/nginx/passwd.db 
chmod 400 /usr/local/nginx/passwd.db 

2、修改主配置文件相對應目錄,添加認證配置項

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;
        }

3、重啓服務,訪問測試

nginx -t
systemctl restart nginx

瀏覽器訪問 http://www.gcc.com
在這裏插入圖片描述

六、基於客戶端的訪問控制

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


vim /usr/local/nginx/conf/nginx.conf
 location / {
   
     
            root   html;
            index  index.html index.htm;
            deny 192.168.200.100;      #添加拒絕訪問的客戶端的IP
            allow all;        #添加允許其他IP客戶端訪問
        }

此時通過IP爲192.168.200.100的虛擬Windows10主機訪問192.168.200.50時就不能訪問。
在這裏插入圖片描述

七、基於域名的 Nginx 虛擬主機

1、爲虛擬主機提供域名解析

echo "192.168.200.50 www.benet.com www.gcc.com" >> /etc/hosts

2、爲虛擬主機準備網頁文檔

mkdir -p /var/www/html/benet
mkdir -p /var/www/html/gcc
echo "this is  dabao" >  /var/www/html/benet/index.html
echo "this is erbao" >  /var/www/html/gcc/index.html

3、修改 Nginx的配置文件


server {
   
     
        listen       80;
        server_name  www.dog.com;	#將localhost改爲www.dog.com
        charset utf-8;				#開啓字符集,修改爲utf-8
        access_log  logs/www.dog.com.access.log;	#開啓訪問日誌,添加www.dog.com,刪除 ‘main’
        location / {
   
     
            root   /var/www/html/dog;	#站點首頁文件指向剛剛創建的文件目錄
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
   
     			#錯誤提示50X
            root   html;				#錯誤提示來自站點html(nginx工作目錄的相對路徑)
        }
    }

#複製以上信息,粘貼並進行簡單修改
server {
   
     
        listen       80;
        server_name  www.benet.com;		#修改域名
        charset utf-8;					#字符集
        access_log  logs/www.benet.com.access.log;	#訪問日誌名改爲www.benet.com
        location / {
   
     
            root   /var/www/html/nenet;	#站點首頁指向benet文件目錄
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
   
     
            root   html;
        }


nginx -t		#驗證語法
service nginx restart	#重啓服務

八、基於端口的虛擬主機

1、創建8080端口的測試網頁

mkdir -p /var/www/html/gcc8080
echo "this is gcc 8080 web" > /var/www/html/gcc8080/index.html
cat /var/www/html/gcc8080/index.html
this is gcc 8080 web

2、修改nginx主配置文件,僅修改監聽端口

    server {
   
     
        listen       192.168.200.50:80;	#修改監聽地址
        server_name  www.benet.com;
        charset utf-8;
        access_log  logs/www.benet.com.access.log;
        location / {
   
      
            root   /var/www/html/benet;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
   
     
            root   html;
        }
}
#複製以上信息,修改部分內容
    server {
   
     
        listen       192.168.200.50:8080;	#修改監聽端口
        server_name  www.benet.com;
        charset utf-8;
        access_log  logs/www.gcc8080.com.access.log;	#修改訪問日誌文件名
        location / {
   
     
            root   /var/www/html/gcc8080;	#修改站點首頁文件目錄名
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
   
     
            root   html;
        }
}


nginx -t		#檢查語法
systemctl restart nginx

九、基於不同IP的虛擬主機

1、添加網卡,修改域名、IP

ifconfig ens33:0 192.168.200.100 netmask 255.255.255.0
--------------------------------------------------------
vim /usr/local/nginx/conf/nginx.conf
·   server {
   
     
        listen       192.168.200.50:80; #只要修改監聽的地址即可(ens33)
        server_name  www.benet.com;
        charset utf-8;
        access_log  logs/www.benet.com.access.log;
        location / {
   
     
            root   /var/www/html/benet;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
   
     
            root   html;
        }
}

    server {
   
     
        listen       192.168.200.100:80;	#地址指向ens36
        server_name  www.gcc.com;
        charset utf-8;
        access_log  logs/www.gcc.com.access.log;
        location / {
   
     
            root   /var/www/html/gcc;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
   
     
            root   html;
        }
    }


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