20190314 Nginx:編譯安裝、Location的使用、常用變量

Nginx
是一個高性能的HTTP和反向代理服務。是一款輕量級的Web服務器和反向代理服務器及電子郵件代理服務器,特點:佔有內存少,併發能力強,
20190314  Nginx:編譯安裝、Location的使用、常用變量

20190314  Nginx:編譯安裝、Location的使用、常用變量

20190314  Nginx:編譯安裝、Location的使用、常用變量

20190314  Nginx:編譯安裝、Location的使用、常用變量

epoll:
在Linux 2.6內核中提出的select和poll的增強版本
支持水平觸發LT和邊緣觸發ET,最大的特點在於邊緣觸發,它只告訴進程哪些fd剛剛變爲就需態,並且只會通知一次
使用“事件”的就緒通知方式,通過epoll_ctl註冊fd,一旦該fd就緒,內核就會採用類似callback的回調機制來激
活該fd,epoll_wait便可以收到通知
優點:
沒有最大併發連接的限制:能打開的FD的上限遠大於1024(1G的內存能監聽約10萬個端口),具體查
看/proc/sys/fs/file-max,此值和系統內存大小相關
效率提升:非輪詢的方式,不會隨着FD數目的增加而效率下降;只有活躍可用的FD纔會調用callback函數,即epoll
最大的優點就在於它只管理“活躍”的連接,而跟連接總數無關
內存拷貝,利用mmap(Memory Mapping)加速與內核空間的消息傳遞;即epoll使用mmap減少複製開銷

基礎特性:
特性:
模塊化設計,1、較好的擴展性 2、高可靠性 3、支持熱部署:不停機更新配置文件,升級版本,更換日誌文件
4、低內存消耗:10000個keep-alive連接模式下的非活動連接,僅需2.5M內存
基本功能:
1、靜態資源的web服務器 2、http協議反向代理服務器 3、pop3/imap4協議反向代理服務器 4、FastCGI(LNMP),uWSGI(python)等協議 5、模塊化(非DSO),如zip,SSL模
Nginx是多進程組織模型,而且是一個由Master主進程和Worker工作進程組

Nginx的安裝:
一是yum的版本比較舊,二是編譯安裝可以更方便自定義相關路徑,三是使用源碼編譯可以自定義相關功能,更方便業務的上的使用,源碼安裝需要提前準備標準的編譯器,GCC的全稱是(GNU Compiler collection),其有GNU開發,並以GPL即LGPL許可,是自由的類UNIX即蘋果電腦Mac OS X操作系統的標準編譯器,因爲GCC原本只能處理C語言,所以原名爲GNU C語言編譯器,後來得到快速發展,可以處理C++,Fortran,pascal,objective-C,java以及Ada等其他語言,此外還需要Automake工具,以完成自動創建Makefile的工作,Nginx的一些模塊需要依賴第三方庫,比如pcre(支持rewrite),zlib(支持gzip模塊)和openssl(支持ssl模塊)等。

使用安裝完成的二進制文件nginx:
[root@s1 ~]# nginx -h
nginx version: nginx/1.12.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit #顯示版本和編譯參數
-t : test configuration and exit #測試配置文件是否異常
-T : test configuration, dump it and exit #測試並打印
-q : suppress non-error messages during configuration testing #靜默模式
-s signal : send signal to a master process: stop, quit, reopen, reload #發送信號
-p prefix : set prefix path (default: /usr/share/nginx/) #指定Nginx 目錄
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #配置文件路徑
-g directives : set global directives out of configuration file #設置全局指令

Nginx的啓動腳本:
yum安裝:官方網站:https://nginx.org/packages/centos/7/x86_64/RPMS/
wget https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm
yum install nginx-1.14.2-1.el7_4.ngx.x86_64.rpm
默認啓動腳本:
[root@centos7 ~]#vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target

默認配置文件:/etc/nginx/nginx.conf
[root@centos7 ~]#grep -v "#" /etc/nginx/nginx.conf | grep -v "^$"
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/
.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
servername ;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}

systemctl start nginx 即可登錄了。

編譯安裝:
150:yum安裝,200:編譯安裝
官方網站:https://nginx.org/en/download.html
[root@centos7 src]cd /usr/local/src 源碼包一般都存放於此
[root@centos7 src]#wget https://nginx.org/download/nginx-1.14.2.tar.gz
[root@centos7 src]#tar xvf nginx-1.14.2.tar.gz
[root@centos7 src]#cd nginx-1.14.2/
[root@centos7 nginx-1.14.2]#yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate
gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel
net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2
libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed

[root@centos7 nginx-1.14.2]#ll
total 732
drwxr-xr-x. 6 1001 1001 4096 Mar 15 11:07 auto
-rw-r--r--. 1 1001 1001 288742 Dec 4 22:52 CHANGES
-rw-r--r--. 1 1001 1001 440121 Dec 4 22:52 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 Mar 15 11:07 conf
-rwxr-xr-x. 1 1001 1001 2502 Dec 4 22:52 configure
drwxr-xr-x. 4 1001 1001 72 Mar 15 11:07 contrib
drwxr-xr-x. 2 1001 1001 40 Mar 15 11:07 html
-rw-r--r--. 1 1001 1001 1397 Dec 4 22:52 LICENSE
drwxr-xr-x. 2 1001 1001 21 Mar 15 11:07 man
-rw-r--r--. 1 1001 1001 49 Dec 4 22:52 README
drwxr-xr-x. 9 1001 1001 91 Mar 15 11:07 src

[root@centos7 nginx-1.14.2]#./configure --prefix=/apps/nginx
[root@centos7 nginx-1.14.2]#make / make install

[root@centos7 nginx-1.14.2]#ll /apps/
total 0
drwxr-xr-x. 6 root root 54 Mar 15 11:20 nginx
[root@centos7 nginx-1.14.2]#ll /apps/nginx/
total 4
drwxr-xr-x. 2 root root 4096 Mar 15 11:20 conf 存放配置文件
drwxr-xr-x. 2 root root 40 Mar 15 11:20 html 存放靜態頁面的目錄
drwxr-xr-x. 2 root root 6 Mar 15 11:20 logs 存放日誌
drwxr-xr-x. 2 root root 19 Mar 15 11:20 sbin 存放可執行程序

[root@centos7 nginx-1.14.2]#./configure --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

[root@centos7 nginx-1.14.2]#/apps/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@centos7 nginx-1.14.2]#/apps/nginx/sbin/nginx
[root@centos7 nginx-1.14.2]#/apps/nginx/sbin/nginx -s stop
現在在150主機上:
scp /usr/lib/systemd/system/nginx.service 172.18.9.200:/usr/lib/systemd/system/nginx.service
200主機:
[root@200 nginx-1.14.2]#vim /usr/lib/systemd/system/nginx.service
[Service]
#PIDFile=/var/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf 把配置文件路徑更改
[root@200 nginx-1.14.2]#systemctl daemon-reload
[root@200 nginx-1.14.2]#systemctl start nginx

[root@200 nginx-1.14.2]#vim /apps/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2; 2個進程
worker_cpu_affinity 0001 0010;
[root@200 nginx-1.14.2]#/apps/nginx/sbin/nginx -s reload
[root@200 nginx-1.14.2]#ps -ef |grep nginx
root 34192 1 0 16:33 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx
nginx 34321 34192 0 16:43 ? 00:00:00 nginx: worker process
nginx 34322 34192 0 16:43 ? 00:00:00 nginx: worker process
root 34324 20781 0 16:43 pts/0 00:00:00 grep --color=auto nginx
[root@200 nginx-1.14.2]#cat /apps/nginx/html/ index.html
172.18.9.200

20190314  Nginx:編譯安裝、Location的使用、常用變量

此時Nginx配置完成可以用了。

下午第一節:
Nginx的默認配置文件:
[root@200 nginx]#grep -v "#" /apps/nginx/conf/nginx.conf | grep -v "^$" 將空行等過濾掉
user nginx nginx;
worker_processes 2;
worker_cpu_affinity 0001 0010;
pid logs/nginx.pid;
events {
worker_connections 1024;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

[root@200 nginx]#cd /apps/nginx/
[root@200 nginx]#ll
total 4
drwx------. 2 nginx root 6 Mar 15 13:49 client_body_temp
drwxr-xr-x. 2 root root 4096 Mar 15 17:28 conf
drwx------. 2 nginx root 6 Mar 15 13:49 fastcgi_temp
drwxr-xr-x. 2 root root 40 Mar 15 16:50 html
drwxr-xr-x. 2 root root 58 Mar 15 17:13 logs
drwx------. 2 nginx root 6 Mar 15 13:49 proxy_temp
drwxr-xr-x. 2 root root 36 Mar 15 15:40 sbin
drwx------. 2 nginx root 6 Mar 15 13:49 scgi_temp
drwx------. 2 nginx root 6 Mar 15 13:49 uwsgi_temp
[root@200 nginx]#cd conf
[root@200 conf]#ll
total 68
-rw-r--r--. 1 root root 1077 Mar 15 13:47 fastcgi.conf
-rw-r--r--. 1 root root 1077 Mar 15 15:40 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 Mar 15 13:47 fastcgi_params
-rw-r--r--. 1 root root 1007 Mar 15 15:40 fastcgi_params.default
-rw-r--r--. 1 root root 2837 Mar 15 15:40 koi-utf
-rw-r--r--. 1 root root 2223 Mar 15 15:40 koi-win
-rw-r--r--. 1 root root 5170 Mar 15 13:47 mime.types
-rw-r--r--. 1 root root 5170 Mar 15 15:40 mime.types.default
-rw-r--r--. 1 root root 2705 Mar 15 17:28 nginx.conf
-rw-r--r--. 1 root root 2656 Mar 15 15:40 nginx.conf.default
-rw-r--r--. 1 root root 636 Mar 15 13:47 scgi_params
-rw-r--r--. 1 root root 636 Mar 15 15:40 scgi_params.default
-rw-r--r--. 1 root root 664 Mar 15 13:47 uwsgi_params
-rw-r--r--. 1 root root 664 Mar 15 15:40 uwsgi_params.default
-rw-r--r--. 1 root root 3610 Mar 15 15:40 win-utf
[root@200 conf]#vim mime.types
types {
text/html html htm shtml; 當訪問互聯網時,網絡會根據這些後綴來解析。
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;

text/mathml                                      mml;
text/plain                                       txt;
text/vnd.sun.j2me.app-descriptor                 jad;
text/vnd.wap.wml                                 wml;
text/x-component                                 htc;

image/png                                        png;
image/svg+xml                                    svg svgz;
image/tiff                                       tif tiff;
image/vnd.wap.wbmp                               wbmp;
image/webp                                       webp;
image/x-icon                                     ico;
image/x-jng                                      jng;
image/x-ms-bmp                                   bmp;

application/font-woff                            woff;
application/java-archive                         jar war ear;
application/json                                 json;
application/mac-binhex40                         hqx;

[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf
http {
server {
listen 172.18.9.200:80;
listen 8080;
}
[root@centos7 ~]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@centos7 ~]#/apps/nginx/sbin/nginx -s reload
[root@centos7 ~]#/apps/nginx/sbin/nginx -s stop 關停重啓後,8080纔會出現。
[root@centos7 ~]#/apps/nginx/sbin/nginx
[root@centos7 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :111 :
LISTEN 0 128
:8080 :
LISTEN 0 128 172.18.9.200:80
3.1:全局配置:
user nginx nginx; #啓動Nginx工作進程的用戶和組worker_processes [number | auto]; #啓動Nginx工作進程的數量worker_cpu_affinity 00000001 00000010 00000100 00001000; #將Nginx工作進程綁定到指定的CPU核心,默認Nginx是不進行進程綁定的,綁定並不是意味着當前nginx進程獨佔以一核心CPU,但是可以保證此進程不會運行在其他核心上,這就極大減少了nginx的工作進程在不同的cpu核心上的來回跳轉,減少了CPU對進程的資源分配與回收以及內存管理等,因此可以有效的提升nginx服務器的性能。[root@s2 ~]# ps axo pid,cmd,psr | grep nginx20061 nginx: master process /apps 020062 nginx: worker process 020063 nginx: worker process 120097 grep --color=auto nginx 0123456

第二題:Location做訪問路徑匹配訪問不同頁面顯示不同的內容
一、[root@200 conf]#ll /apps/nginx/
total 4
drwx------. 2 nginx root 6 Mar 15 13:49 client_body_temp
drwxr-xr-x. 2 root root 4096 Mar 15 17:56 conf
drwx------. 2 nginx root 6 Mar 15 13:49 fastcgi_temp
drwxr-xr-x. 2 root root 40 Mar 15 16:50 html
drwxr-xr-x. 2 root root 58 Mar 15 17:45 logs
drwx------. 2 nginx root 6 Mar 15 13:49 proxy_temp
drwxr-xr-x. 2 root root 36 Mar 15 15:40 sbin
drwx------. 2 nginx root 6 Mar 15 13:49 scgi_temp
drwx------. 2 nginx root 6 Mar 15 13:49 uwsgi_temp
[root@200 conf]#ll /apps/nginx/html 存放訪問頁面的文件
total 8
-rw-r--r--. 1 root root 537 Mar 15 13:47 50x.html
-rw-r--r--. 1 root root 13 Mar 15 13:57 index.html

二、配置文件中的location部分:
[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;

    #redirect server error pages to the static page /50x.html
    #error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    #proxy the PHP scripts to Apache listening on 127.0.0.1:80

    #location ~ \.php$ {
    #proxy_pass   http://127.0.0.1;
    #}

    #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #location ~ \.php$ {
    #root           html;
    #fastcgi_pass   127.0.0.1:9000;
    #fastcgi_index  index.php;
    #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #include        fastcgi_params;
    #}

[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf 此文件中的events中添加2項,設置爲on
events {
worker_connections 1024;
use epoll;
multi_accept on;
accept_mutex on; }
[root@centos7 ~]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@centos7 ~]#/apps/nginx/sbin/nginx -s reload
[root@centos7 ~]#ps -ef |grep nginx 顯示如下:
root 35110 1 0 17:45 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx
nginx 35111 35110 0 17:45 ? 00:00:00 nginx: worker process
nginx 35112 35110 0 17:45 ? 00:00:00 nginx: worker process
root 35596 24183 0 18:20 pts/1 00:00:00 grep --color=auto nginx

三、在c盤的hosts中加入“172.18.9.200 www.magedu.net”
[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf
將“include /apps/nginx/conf.d/*.conf;”加在文末HTTP文檔中
[root@centos7 ~]#mkdir /apps/nginx/conf/conf.d
[root@centos7 ~]#cd /apps/nginx/conf/conf.d
[root@centos7 conf.d]#ll
total 0
[root@centos7 conf.d]#vim pc.conf 先建起一個,可以先用着。
server {
listen 80;
server_name www.magedu.net;
location / {
root /data/nginx/html/pc;
}
#location /about {
#root /data/nginx/html/pc;
#index index.html;
}
}

[root@200 ~]#cd /apps/nginx/conf/conf.d
[root@200 conf.d]#mkdir /data/nginx/html/pc -p
[root@200 conf.d]#vim /data/nginx/html/pc/index.html
pc web
去訪問www.magedu.net
20190314  Nginx:編譯安裝、Location的使用、常用變量

[root@200 conf.d]#cp pc.conf mobile.conf
[root@200 conf.d]#vim mobile.conf
server {
listen 80;
server_name mobile.magedu.net;
location / {
root /data/nginx/html/mobile; }
#location /about {
#root /data/nginx/html/pc;
#index index.html;
#}
}
[root@200 conf.d]#/apps/nginx/sbin/nginx -t
[root@200 conf.d]#/apps/nginx/sbin/nginx -s reload
[root@200 conf.d]#mkdir /data/nginx/html/mobile
[root@200 conf.d]#vim /data/nginx/html/mobile/index.html
mobile web
此時去訪問mobile.magedu.net

[root@200 conf.d]#vim /apps/nginx/conf/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.net;
location / {
root /data/nginx/html/pc;
}
location /about { 註釋去掉
root /data/nginx/html/pc;
index index.html;
}
}
[root@200 conf.d]#mkdir /data/nginx/html/pc/about
[root@200 conf.d]#vim /data/nginx/html/pc/about/index.html
about page
20190314  Nginx:編譯安裝、Location的使用、常用變量

四、Nginx的location的配置:
location的詳細使用:
= #用於標準uri前,需要請求字串與uri精確匹配,如果匹配成功就停止向下匹配並立即處理請求。
~ #區分大小寫
~ #不區分大寫
!~ #區分大小寫不匹配
!~
#不區分大小寫不匹配
^~ #匹配以什麼開頭
$ #匹配以什麼結尾
\ #轉義字符。可以轉. * ?等

  • #代表任意長度的任意字符
    舉例1:
    1、[root@200 images]#mv 7c72236492a311ba7e81b4044a405615.jpg 1.jpg
    [root@200 images]#ll
    total 180
    -rw-r--r--. 1 root root 49584 Mar 15 20:23 1.jpg
    2、[root@centos7 conf.d]#vim /apps/nginx/conf/conf.d/pc.conf

server {
location ~ /1.jpg {
root /data/nginx/html/images; }
}
[root@centos7 conf.d]# /apps/nginx/sbin/nginx -t
[root@centos7 conf.d]# /apps/nginx/sbin/nginx -s reload
3、
20190314  Nginx:編譯安裝、Location的使用、常用變量

舉例2:^~匹配以什麼開頭
[root@200 conf.d]#cd /data/nginx/html/pc
[root@200 pc]#mkdir images images
[root@200 pc]#mkdir images images1
[root@200 pc]#vim images/index.html
images
[root@200 pc]#vim images1/index.html
images1
[root@centos7 conf.d]#vim /apps/nginx/conf/conf.d/pc.conf
server {
location ^~ /images {
root /data/nginx/html/pc;
index index.html;
}

location^~/images1{
root /data/nginx/html/pc;
index index.html;
}
}

20190314  Nginx:編譯安裝、Location的使用、常用變量

20190314  Nginx:編譯安裝、Location的使用、常用變量

簡單的web站點:一個Nginx、2個後端服務器、一臺mysql數據庫、一臺NFS共享存儲服務器

20190314  Nginx:編譯安裝、Location的使用、常用變量

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