溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Nginx優(yōu)化

發(fā)布時(shí)間:2020-04-05 13:42:03 來(lái)源:網(wǎng)絡(luò) 閱讀:313 作者:mb5d03569e7eb16 欄目:系統(tǒng)運(yùn)維

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

一.nginx簡(jiǎn)介

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

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

二.nginx的核心特點(diǎn)

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

Nginx優(yōu)化

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

三.nginx平滑升級(jí)(案例)

下載軟件包

[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 重新打開(kāi)日志文件
USR2 平滑升級(jí)可執(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ù)實(shí)際情況修改為自己想要的信息
#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;                //與上一個(gè)文件中修改的名稱(chēng)一樣(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                     //注意與上兩個(gè)文件中修改的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                        //開(kāi)啟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

五.實(shí)現(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 {              \\從這里開(kāi)始加
        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選項(xiàng)的作用(案例)

  • alias與root的區(qū)別
root:實(shí)際訪問(wèn)的文件會(huì)被拼接URL的路徑
alias:實(shí)際訪問(wèn)的文件路徑不會(huì)拼接URL的路徑
  • 使用root
    [root@localhost /]# vim /usr/local/nginx/conf/nginx.conf
    location ^~ /www {
            root   /var/www/html; 
                        //當(dāng)訪問(wèn)192.168.148.130/www/時(shí),尋找/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
  • 訪問(wèn)如下:
    Nginx優(yōu)化

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

  • 匹配指定的后綴時(shí),就重定向到指定的文件
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf
 location ~*  \.(gif|jpg|jpeg|png|css|js|ico)$ {
        root /webroot/res/;  
                \\當(dāng)訪問(wèn)以上內(nèi)容結(jié)尾的頁(yè)面就會(huì)去找//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
  • 訪問(wèn)如下:

Nginx優(yōu)化

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

     location ~* .(gif|jpg|jpeg|png|css|js|ico)$ { 
        \\這個(gè)加入上面的命令上面,要不容易報(bào)錯(cuò)
        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
  • 訪問(wèn)如下:

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

七.配置https訪問(wèn)nginx

  • 開(kāi)啟nginx的https功能,需要編譯是添加選項(xiàng)- -with-http_ ssl module

  • http與https區(qū)別
    ht tp使用端口號(hào)為80,數(shù)據(jù)傳輸時(shí)使用明文傳輸,
    https使用443端口,數(shù)據(jù)傳輸時(shí)使用密文傳輸,需要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 {                      \\原來(lái)的server中添加如下:
        listen       443 ssl;
        server_name  localhost;
        ssl_certificate      /usr/local/nginx/ca/ca.crt;  \\證書(shū)存放的路徑
        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;
  • 訪問(wèn)如下:

Nginx優(yōu)化

Nginx優(yōu)化

  • 開(kāi)啟認(rèn)證

nginx使用http2版本,需要nginx版本在1. 10以上,安裝時(shí)需要添加編譯選項(xiàng)--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

//開(kāi)啟nginx認(rèn)證頁(yè)面 需要使用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中添加第二個(gè)用戶,需要省略“-c”選項(xiàng),否則會(huì)覆蓋之前的所有用戶。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 
    ...............              //省略部分內(nèi)容 
        location / {
            root   html;
            index  index.html index.htm;
            auth_basic "請(qǐng)輸入登錄賬號(hào)";     //添加提示語(yǔ)句
            auth_basic_user_file /usr/local/nginx/.passwd;    //認(rèn)證信息存放路徑
        }
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI