Nginx優(yōu)化

博文結(jié)構(gòu)
Nginx介紹
Nginx的核心特點
Nginx平滑升級
修改Nginx版本信息
Nginx虛擬主機(jī)配置
nginx配置文件location選項的作用
配置https訪問nginx
開啟Nginx訪問認(rèn)證

創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,先為都江堰等服務(wù)建站,都江堰等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為都江堰企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

一.nginx簡介

Nginx是一款輕量級的網(wǎng)頁服務(wù)器、反向代理服務(wù)器以及電子郵件代理服務(wù)器。因它的穩(wěn)定性、豐富的功能集、實例配置文件和低系統(tǒng)資源消耗而聞名。

Nginx已經(jīng)在俄羅斯最大的門戶網(wǎng)站上運行,同時俄羅斯有超過20%的虛擬主機(jī)平臺采用Nginx作為反向代理服務(wù)器;在國內(nèi),Nginx已經(jīng)運行在淘寶、新浪、網(wǎng)易等多家網(wǎng)站使用Nginx作為Web服務(wù)器或反向代理服務(wù)器。

二.nginx的核心特點

(1)跨平臺:Nginx 可以在大多數(shù) OS 編譯運行,而且也有 Windows 的版本
(2)配置異常簡單:非常容易上手
(3)非阻塞、高并發(fā)連接:官方測試能夠支撐 5 萬并發(fā)連接,在實際生產(chǎn)環(huán)境中跑到 2~3 萬并發(fā)連接數(shù)。(這得益于 Nginx 使用了最新的 epoll 模型)
(4)事件驅(qū)動:采用 epoll 模型,支持更大的并發(fā)連接
(5)Master/Worker 結(jié)構(gòu):一個 master 進(jìn)程,生成一個或多個 worker 進(jìn)程

Nginx優(yōu)化

(6)內(nèi)存消耗?。禾幚泶蟛l(fā)的請求內(nèi)存消耗非常小。在 3 萬并發(fā)連接下,開啟的 10 個 Nginx 進(jìn)程才消耗 150M 內(nèi)存(15Mx10=150M)
(7)內(nèi)置的健康檢查功能:如果 Nginx 代理的后端的某臺 Web 服務(wù)器宕機(jī)了,不會影響 前端訪問。 
(8)節(jié)省帶寬:支持 GZIP 壓縮,可以添加瀏覽器本地緩存的 Header 頭。 (9)穩(wěn)定性高:用于反向代理,宕機(jī)的概率微乎其微

三.nginx平滑升級(案例)

下載軟件包

[root@localhost ~]# yum -y install pcre-devel openssl-devel
[root@localhost ~]# tar zxf nginx-1.14.0.tar.gz 
[root@localhost ~]# tar zxf nginx-1.2.4.tar.gz 
[root@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module && make && make install
[root@localhost nginx-1.14.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.14.0]# nginx
[root@localhost nginx-1.14.0]# useradd nginx -s /sbin/nologin -M
[root@localhost nginx-1.2.4]# cd nginx-1.2.4/
[root@localhost nginx-1.2.4]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module && make
[root@localhost nginx-1.2.4]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
[root@localhost nginx-1.2.4]# cp objs/nginx /usr/local/nginx/sbin/
[root@localhost nginx-1.2.4]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:            LISTEN      17739/nginx: master 
[root@localhost nginx-1.2.4]# kill -USR2 17739
[root@localhost nginx-1.2.4]# nginx -s reload
[root@localhost ~]# kill -QUIT 17739            
////平滑的關(guān)閉舊版的nginx進(jìn)程
[root@localhost nginx-1.2.4]# nginx -V  \\查看版本
nginx version: nginx/1.2.4
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) 
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module
  • 關(guān)于nginx使用kill命令常用的參數(shù):
QUIT 平滑關(guān)閉
HUP 平滑重啟,重新加載配置文件
USR1 重新打開日志文件
USR2 平滑升級可執(zhí)行程序
WINCH 平滑關(guān)閉工作進(jìn)程

四.修改nginx版本信息

[root@localhost ~]# vim /usr/src/nginx-1.2.4/src/core//nginx.h
                     ………………          //省略部分內(nèi)容
#define nginx_version      1002004
#define NGINX_VERSION      "8.8.8.8"                       //根據(jù)實際情況修改為自己想要的信息
#define NGINX_VER          "xws/" NGINX_VERSION            //同上,注意修改完的lzj
[root@localhost ~]# vim /usr/src/nginx-1.2.4/src/http/ngx_http_header_filter_module.c 
                     ………………          //省略部分內(nèi)容
static char ngx_http_server_string[] = "Server: xws" CRLF;                //與上一個文件中修改的名稱一樣(lzj)
static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
[root@localhost ~]# vim /usr/src/nginx-1.2.4/src/http/ngx_http_special_response.c 
                     ………………          //省略部分內(nèi)容
                     static u_char ngx_http_error_tail[] =
"<hr><center>xws</center>" CRLF                     //注意與上兩個文件中修改的lzj要一致
"</body>" CRLF
"</html>" CRLF
;
[root@localhost ~]# cd /usr/src/nginx-1.2.4/
[root@localhost nginx-1.2.4]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module && make
[root@localhost ~]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
[root@localhost ~]# cp /usr/src/nginx-1.2.4/objs/nginx /usr/local/nginx/sbin/
[root@localhost ~]# nginx -s stop                //停止nginx服務(wù)
[root@localhost ~]# nginx                        //開啟nginx服務(wù)
[root@localhost ~]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: xws/8.8.8.8                              //查看版本信息
Date: Sat, 30 Nov 2019 15:06:32 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 30 Nov 2019 14:42:09 GMT
Connection: keep-alive
Accept-Ranges: bytes

五.實現(xiàn)nginx虛擬主機(jī)(不同域名相同ip)

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf \\在http中加入以下
http {
    include       mime.types;
    default_type  application/octet-stream;
        server {              \\從這里開始加
        listen    80;
        server_name      www.accp.com;
        location / {
        root /accp;
        index index.html index.htm;
        }
        }
    server {
        listen    80;
        server_name      www.bdqn.com;
        location / {
        root /bdqn;
        index index.html index.htm;
        }
        }

[root@localhost ~]# mkdir /bdqn
[root@localhost ~]# mkdir /accp
[root@localhost ~]# vim /bdqn/index.html
www.bdqn.com
[root@localhost ~]# vim /accp/index.html
www.accp.com
[root@localhost ~]# nginx -s reload
[root@localhost ~]# vim /etc/hosts
192.168.148.130         www.bdqn.com
192.168.148.130         www.accp.com
[root@localhost ~]# curl www.bdqn.com
www.dbqn.com
[root@localhost ~]# curl www.accp.com
www.accp.com

六.nginx配置文件location選項的作用(案例)

  • alias與root的區(qū)別
root:實際訪問的文件會被拼接URL的路徑
alias:實際訪問的文件路徑不會拼接URL的路徑
  • 使用root
    [root@localhost /]# vim /usr/local/nginx/conf/nginx.conf
    location ^~ /www {
            root   /var/www/html; 
                        //當(dāng)訪問192.168.148.130/www/時,尋找/var/www/html下的www目錄下的文件
            index  index.html index.htm;
        }
    [root@localhost /]# mkdir -p /var/www/html/www/
    [root@localhost /]# vim /var/www/html/www/index.html
    www
    [root@localhost /]# nginx -s reload
  • 訪問如下:
    Nginx優(yōu)化

  • 使用alias
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf
location ^~ /test {
            alias   /var/www/html;
                        \\尋找/var/www/html下的網(wǎng)頁文件
            index  index.html index.htm;
        }
[root@localhost /]# vim /var/www/html/index.html
test
[root@localhost /]# nginx -s reload
  • 訪問如下:
    Nginx優(yōu)化

  • 匹配指定的后綴時,就重定向到指定的文件
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf
 location ~*  \.(gif|jpg|jpeg|png|css|js|ico)$ {
        root /webroot/res/;  
                \\當(dāng)訪問以上內(nèi)容結(jié)尾的頁面就會去找//webroot/res路徑下的文件
        index index.html index.htm;
        }

[root@localhost /]# mkdir /webroot/res -p
[root@localhost /]# mv u=3485698722\,1702346544\&fm\=26\&gp\=0.jpg /webroot/res/a.png
[root@localhost /]# ls /webroot/res
a.png
[root@localhost /]# nginx -s reload
  • 訪問如下:

Nginx優(yōu)化

[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf

     location ~* .(gif|jpg|jpeg|png|css|js|ico)$ { 
        \\這個加入上面的命令上面,要不容易報錯
        rewrite .(gif|jpg) /error.png;
        }
[root@localhost /]# mv xxx.jfif /usr/local/nginx/html/error.png
[root@localhost /]# ls /usr/local/nginx/html/
[root@localhost /]# nginx -s reload
  • 訪問如下:

Nginx優(yōu)化
Nginx優(yōu)化

七.配置https訪問nginx

  • 開啟nginx的https功能,需要編譯是添加選項- -with-http_ ssl module

  • http與https區(qū)別
    ht tp使用端口號為80,數(shù)據(jù)傳輸時使用明文傳輸,
    https使用443端口,數(shù)據(jù)傳輸時使用密文傳輸,需要ca數(shù)字簽名才能完成
[root@localhost ca]# mkdir /usr/local/nginx/ca
[root@localhost ca]# cd /usr/local/nginx/ca/
[root@localhost ca]# openssl genrsa -out ca.key 4096
\\一下內(nèi)容隨便填
Generating RSA private key, 4096 bit long modulus
......................................++
.......................++
e is 65537 (0x10001)
[root@localhost ca]# openssl req -new -x509 -days 7304 -key ca.key -out ca.crt
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:aa
State or Province Name (full name) []:cc
Locality Name (eg, city) [Default City]:vv
Organization Name (eg, company) [Default Company Ltd]:as
Organizational Unit Name (eg, section) []:df
Common Name (eg, your name or your server's hostname) []:ff
Email Address []:asd
[root@localhost ca]# ls
ca.crt  ca.key
[root@localhost ca]# nginx -s stop 
[root@localhost ca]# nginx 
[root@localhost ca]# vim /usr/local/nginx/conf/nginx.conf

    server {                      \\原來的server中添加如下:
        listen       443 ssl;
        server_name  localhost;
        ssl_certificate      /usr/local/nginx/ca/ca.crt;  \\證書存放的路徑
        ssl_certificate_key  /usr/local/nginx/ca/ca.key;  \\密鑰存放路徑
        ssl_session_cache       shared:SSL:1m;
        ssl_session_timeout     5m;
        access_log      /usr/local/nginx/logs/https-access.log;
  • 訪問如下:

Nginx優(yōu)化

Nginx優(yōu)化

  • 開啟認(rèn)證

nginx使用http2版本,需要nginx版本在1. 10以上,安裝時需要添加編譯選項--with-http v2 module
http2.0版本只能在https上使用,修改上- -步配置文件listen 443 ssl http2;

  • 所以在這之前需要重新編譯nginx
[root@localhost nginx-1.14.0]# cd nginx-1.14.0/
[root@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module
[root@localhost nginx-1.14.0]# make

//開啟nginx認(rèn)證頁面 需要使用htpasswd命令生成用戶信息

[root@localhost /]# yum -y install httpd-tools
[root@localhost ~]# htpasswd -c /usr/local/nginx/.passwd xws
New password: 
Re-type new password: 
Adding password for user xws
//用戶認(rèn)證信息存放路徑是/usr/local/nginx/.passwd
//若要向.passwd中添加第二個用戶,需要省略“-c”選項,否則會覆蓋之前的所有用戶。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 
    ...............              //省略部分內(nèi)容 
        location / {
            root   html;
            index  index.html index.htm;
            auth_basic "請輸入登錄賬號";     //添加提示語句
            auth_basic_user_file /usr/local/nginx/.passwd;    //認(rèn)證信息存放路徑
        }

新聞名稱:Nginx優(yōu)化
本文地址:http://muchs.cn/article0/pihoio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、App開發(fā)搜索引擎優(yōu)化、做網(wǎng)站、軟件開發(fā)、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化