码迷,mamicode.com
首页 > 其他好文 > 详细

20190314 Nginx:编译安装、Location的使用、常用变量

时间:2019-03-16 23:28:47      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:pack   end   文件中   document   mime   第三方库   轻量级   总数   show   

Nginx
是一个高性能的HTTP和反向代理服务。是一款轻量级的Web服务器和反向代理服务器及电子邮件代理服务器,特点:占有内存少,并发能力强,
技术图片

技术图片

技术图片

技术图片

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

技术图片

此时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
技术图片

[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
技术图片

四、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、
技术图片

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

技术图片

技术图片

简单的web站点:一个Nginx、2个后端服务器、一台mysql数据库、一台NFS共享存储服务器

技术图片

20190314 Nginx:编译安装、Location的使用、常用变量

标签:pack   end   文件中   document   mime   第三方库   轻量级   总数   show   

原文地址:https://blog.51cto.com/14128387/2364120

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!