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的使用、常用变量

在配置文件中加入:这种格式做匹配最常用
[root@200 ~]#vim /apps/nginx/conf/conf.d/pc.conf
location ~* .(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
root /data/nginx/images;
index index.html;
}
[root@200 ~]#/apps/nginx/sbin/nginx -t
[root@200 ~]#/apps/nginx/sbin/nginx -s reload
此时我们即可去访问所对应的各种格式的文件了!!!!

Nginx四层访问控制,基于IP/帐户

访问控制基于模块ngx_http_access_module实现,可以通过匹配客户端源IP地址进行限制。
一、[root@200 ~]#yum install httpd_tools -y
[root@200 ~]#htpasswd -cbm /apps/nginx/conf/.htpassword user1 123gxy
Adding password for user user1
[root@200 ~]#htpasswd -bm /apps/nginx/conf/.htpassword user2 123gxy
Adding password for user user2
[root@200 ~]#tail /apps/nginx/conf/.htpassword
user1:$apr1$S0Lwa3Ja$RLr9H7ILXMCyHJpZ0F8cP.
user2:$apr1$FspSBOm/$qTGj/QI8YzwqhBsMDryHo.
二、[root@s2 ~]# vim /apps/nginx/conf/conf.d/pc.conf
location = /login/ {
root /data/nginx/html/;
index index.html;
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/.htpasswd;
}
三、location /about {
alias /data/nginx/html/;
index index.html;
deny 172.18.9.1;
allow 1722.18.9.0/24;
allow 127.9.0.0/16;
deny all; #先允许小部分,再拒绝大部分
}
访问网页:
20190314  Nginx:编译安装、Location的使用、常用变量

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

编辑login的网页:
vim /apps/nginx/conf/nginx.conf/pc.conf
location /login {
index index.html;
#auth_basic "input password";
#auth_basic_user_file /apps/nginx/conf/ .htpassword;
root /data/nginx/html;
}
/apps/nginx/sbin/nginx -t
/apps/nginx/sbin/nginx -s reload
访问网页如下:
20190314  Nginx:编译安装、Location的使用、常用变量

作为下载服务器配置:
1、mkdir /data/nginx/html/pc/download
2、vim /apps/nginx/conf/conf.d/pc.conf
location /download {
autoindex on; #自动索引功能
autoindex_exact_size on; #计算文件确切大小(单位bytes),off只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; #显示本机时间而非GMT(格林威治)时间
root /data/nginx/html/pc;
}
3、[root@s2 pc]# cp /root/anaconda-ks.cfg /data/nginx/html/pc/download/
4、重启Nginx并访问测试下载页面:
20190314  Nginx:编译安装、Location的使用、常用变量

worker_processes [number | auto]; #启动Nginx工作进程的数量
worker_cpu_affinity 01 10; #将Nginx工作进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占以一核心CPU,但是可以保证此进程不会运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能。
#错误日志记录配置,语法:error_log file [debug | info | notice | warn | error | crit |alert | emerg]
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log /apps/nginx/logs/error.log error;
#pid文件保存路径
pid /apps/nginx/logs/nginx.pid;
worker_priority 0; #工作进程优先级,-20~19
worker_rlimit_nofile 65536; #这个数字包括Nginx的所有连接(例如与代理服务器的连接等),而不仅仅是与客户端的连接,另一个考虑因素是实际的并发连接数不能超过系统级别的最大打开文件数的限制.
daemon off; #前台运行Nginx服务用于测试、docker等环境。
master_process off|on; #是否开启Nginx的master-woker工作模式。
2、events {
worker_connections 65536;
use epoll; #使用epoll事件驱动,Nginx支持众多的事件驱动,比如select、poll、epoll,只能设置在events模块中设置。
accept_mutex on; #优化同一时刻只有一个请求而避免多个睡眠进程被唤醒的设置,on为防止被同时唤醒默认为off,全部唤醒的过程也成为"惊群",因此nginx刚安装完以后要进行适当的优化。
multi_accept on; Nginx服务器的每个工作进程可以同时接受多个新的网络连接,但是需要在配置文件中配置,此指令默认为关闭,即默认为一个工作进程只能一次接受一个新的网络连接,打开后几个同时接受多个,
3、http {
include mime.types; #导入支持的文件类型
default_type application/octet-stream; #设置默认的类型,会提示下载不匹配的类型文件
#日志配置部分
#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 logs/access.log main;
#自定义优化参数
sendfile on; 指定是否使用sendfile系统调用来传输文件,sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操作),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝,硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈。
#tcp_nopush on; #在开启了sendfile的情况下,合并请求后统一发送给客户端。
#tcp_nodelay off; #在开启了keepalived模式下的连接是否启用TCP_NODELAY选项,当为off时,延迟发送,合并多个请求后再发送,默认On时,不延迟发送,立即发送用户相应报文。
#keepalive_timeout 0;
keepalive_timeout 65 65; #设置会话保持时间
#gzip on; #开启文件压缩
listen 80; #设置监听地址和端口
server_name localhost; #设置server name,可以以空格隔开写多个并支持正则表达式,如
*.magedu.com www.magedu.* ~^www\d+.magedu.com$
#charset koi8-r; #设置编码格式,默认是俄语格式,可以改为utf-8
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;
} #

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

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

转载于:https://blog.51cto.com/14128387/2364120

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值