1. 程式人生 > >CentOS 7 上搭建nginx 1.6

CentOS 7 上搭建nginx 1.6

elinks kcon %d 年月日 日期結尾 bmp 就會 XP c-c

準備要素,編譯環境,創建組,創建被service所管理的腳本,兩種隱藏版本方法,實現主進程用root創建 子進程有nginx 創建,圖片緩存時間

在最新的centos7.3上搭建nginx 1.6.
安裝需要的環境
yum install gcc gcc-c++ pcre-devel zlib-devel -y
yum install elinks -y 
創建用戶組
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
cd /opt/nginx-1.6.0  //準備編譯
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

make && make install  //這個先不著急隱藏版本兩種辦法 這是第一種
隱藏版本信息在make 之前可以進行篡改 在解壓的目錄裏面如我的解壓在opt下路徑 vim/opt/nginx-1.6.0/src/core/nginx.h 
#define nginx_version      1006000
#define NGINX_VERSION      "1.6.0" //修改雙引號裏面的字符串如 "1.1.1.1"

在make 之前竄改版本
技術分享圖片

1、firewalld的基本使用
啟動: systemctl start firewalld
查看狀態: systemctl status firewalld 
停止: systemctl disable firewalld
禁用: systemctl stop firewalld

2.systemctl是CentOS7的服務管理工具中主要的工具,它融合之前service和chkconfig的功能於一體。
啟動一個服務:systemctl start firewalld.service
關閉一個服務:systemctl stop firewalld.service
重啟一個服務:systemctl restart firewalld.service
顯示一個服務的狀態:systemctl status firewalld.service
在開機時啟用一個服務:systemctl enable firewalld.service
在開機時禁用一個服務:systemctl disable firewalld.service
查看服務是否開機啟動:systemctl is-enabled firewalld.service
查看已啟動的服務列表:systemctl list-unit-files|grep enabled
查看啟動失敗的服務列表:systemctl --failed

技術分享圖片

yum install elinks -y  //用於字符界面測試nginx 
elinks http://IP地址/ 測試

第二種隱藏版本的方法就是在安裝後修改nginx 主配置文件

vim /usr/local/nginx/conf/nginx.conf //主配置文件裏面找到 
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off; //插入這段話
#重啟服務 先stop 在start  用curl -I http:// IP/
就會顯示 
HTTP/1.1 200 OK
Server: nginx  //看個人需求
Date: Fri, 15 Jun 2018 14:59:07 GMT

修改用戶和組 ps aux | grep nginx 查看進程
技術分享圖片

vim /usr/local/nginx/conf/nginx.conf //修改主配置文件
user  nginx nginx; //就在開頭 #號註釋掉 換成nginx 
location / {
            root   html;
            index  index.html index.htm;
        } //在下面插入
location ~\.(gif|jpg|jepg|png|bmp|ico)$ {
            root html; //支持的圖片格式
            expires 1d; //靜態圖片緩存為一天
        }
重啟服務 使用抓包工具進行查看

修改首頁 vim/usr/local/nginx/html/index.html

<h1>Welcome to nginx!</h1> //顯示首頁標題
<img src="game.jpg">  //圖片路徑
重啟服務 別忘把圖片復制到 index.html 同一個目錄裏

技術分享圖片
鏈接超時

vim /usr/local/nginx/conf/nginx.conf //默認已經就是開啟的 找到keepalive_timeout 修改 單位秒
keepalive_timeout  65 180; //65 超時時間 180客戶超時時間
client_header_timeout 80; // 請求頭部的超時時間
client_body_timeout 80; // 讀寫內容超時時間 
重啟服務 

日誌分割 要創建腳本並且寫入到周期性計劃任務裏

#!/bin/bash
#Filename:fenge.sh
d=$(date -d "-1 day" "+%Y%m%d")    #顯示一天前的時間
logs_path="/var/log/nginx" #日誌分隔出來的保存路徑
pid_path="/usr/local/nginx/logs/nginx.pid" #日誌進程序列號
[ -d $logs_path ] || mkdir -p $logs_path # 判斷有沒有保存路徑文件存在 若沒有則創建並以前一天年月日的形式
mv /usr/local/nginx/logs/access.log #剪切移動訪問日誌 移動到保存前面備份準備的路徑並且以前一天日期結尾 ${logs_path}/test.com-access.log-$d
kill -USR1 $(cat $pid_path) #殺死進程 則會重新生成新的日誌文件
find $logs_path -mtime +30 | xargs rm -rf # 當存儲到達30 時 則會刪除前30 天的備份文件

技術分享圖片
技術分享圖片

CentOS 7 上搭建nginx 1.6