Nginx的基本功能

1.静态资源的web服务器

2.http协议反向代理服务器

3.tcp/udp协议的请求转发

安装nginx
yum install epel-release
yum install nginx
systemctl start nginx #查看nginx帮助
[root@localhost ~]# nginx -h
nginx version: nginx/1.12. 检查配置文件语法
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

安装配置

Nginx配置文件管理

主配置文件结构: 四大配置段

主配置段即全局配置段,对http,mail,stream都有效

每个段负责一种功能配置,不能把不同的配置段和不同功能混合配置 如 http配置段就只能配置和http协议相关配置而不能配置mail和stream的信息

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; events {
worker_connections 1024;
}

主配置段

http配置段  http/https协议相关配置

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 ;
types_hash_max_size ; include /etc/nginx/mime.types;
default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf; server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html; # Load configuration files for the default server block.
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 {
}
}
}

http配置段

mail配置段  mail协议相关配置段

mail
{ mail 协议相关配置段 }

mail配置段

stream配置段  stream服务器相关配置段

stream
{
stream 服务器相关配置段 }

stream配置段

http协议相关配置

 #nginx对配置文件的配置项目所处的位置要求比较严格,有严格的嵌套关系
# for more information.
http{
#如果需要添加自定义虚拟主机配置,必须在此处指定目录
#因为server必须位于http{}里面
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/conf.d/vhosts/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html; # Load configuration files for the default server block.
#此处不能添加自定义虚拟主机配置文件 这里添加的是对默认虚拟主机的配置
#server{}中不能再添加server{} 否则会报语法错误
include /etc/nginx/default.d/*.conf; location / {
}
}

自定义虚拟主机配置

nginx的虚拟主机根目录不能指向/root目录,必须在nginx用户有权限读取的目录

错误示例:

正确示例:

mkdir /etc/nginx/conf.d/vhosts
[root@localhost vhosts]# ls
a.conf b.conf c.conf
[root@localhost vhosts]# vi a.conf
server {
listen ;
server_name www.a.com;
root /var/www/sitea;
index index.html;
}
[root@localhost vhosts]# vi b.conf
server {
listen ;
index index.html;
server_name www.b.com;
root /var/www/siteb;
}
[root@localhost vhosts]# nginx -s reload [root@localhost www]# pwd
/var/www
[root@localhost www]# ls
sitea siteb sitec
[root@localhost www]# cd sitea
[root@localhost sitea]# ls
index.html

基于主机名配置多个主机

location可以定义匹配到的某个资源位置,针对这个位置做单独的处理
server {
listen ;
server_name www.a.com;
root /var/www/sitea;
index index.html;
location /news {
root /var/www/siteb/;
#这里表示访问www.a.com/news的时候跳转到/www/siteb/news/index.html 并不是直接访问siteb目录下的index.html siteb目录下必须存在news目录 否则会提示404
}
location /news {
alias /var/www/siteb/;
#访问www.a.com/news的时候跳转到/www/siteb/index.html
}
} location /images/
{
如果uri不存在就用default.gif代替返回
try_files $uri /images/default.gif;
}

location和alias配置

hash运算设置网站上传目录

[root@localhost ~]# sha1sum anaconda-ks.cfg
fe9da4faa341470772d182a1e3f42b288ce43f9a anaconda-ks.cfg
[root@localhost ~]# mkdir a/9f//useruploadfile
[root@localhost ~]# echo ** | bc

hash目录

通过对上传的文件名进行哈希运算得到hash值,hash值是一串16进制的字符串.每个字符所占的范围是0-f16种可能,所以如果文件目录设计模式为1-2-2.代表取最后一个字符作为第一级总目录名称,取倒数2和3个字符作为二级目录名称,取倒数4和5个字符作为三级目录名称,总共可以生成100多万的目录组合.每个文件夹存放100个文件就可以存储上亿个文件.

因为文件系统查找文件是通过查找inode表来实现文件定位的,如果把上亿个文件都存储在同一个目录将会大大的降低文件查找效率,所以采取hash值为文件目录的方式会大大提高文件查找效率.

网站压力性能测试

[root@localhost ~]# yum install httpd-tools
[root@localhost ~]# ab -c -n http://www.a.com
ab: invalid URL #不能只写http://www.a.com
Usage: ab [options] [http[s]://]hostname[:port]/path
[root@localhost ~]# ab -c -n http://www.a.com/index.html

ab命令测试

Nginx动态增加编译模块

[root@hz-ds-itssjjd--- ~]# nginx -V
nginx version: nginx/1.14.
built by gcc 4.4. (Red Hat 4.4.-) (GCC)
built with OpenSSL 1.0.1e-fips Feb
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-stream --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-pcre=/usr/local/src/pcre-8.41 --with-zlib=/usr/local/src/zlib-1.2. 在nginx源码的目录下执行
cd /usr/local/src/nginx
./configure --prefix=/usr/local/nginx --with-stream --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-pcre=/usr/local/src/pcre-8.41 --with-zlib=/usr/local/src/zlib-1.2. --add-module=/opt/tools/lua-nginx-module-master [root@hz-ds-itssjjd--- nginx-1.14.]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs objs2 README src
[root@hz-ds-itssjjd--- nginx-1.14.]# pwd
/usr/local/src/nginx-1.14.
[root@hz-ds-itssjjd--- nginx-1.14.]# cd objs
[root@hz-ds-itssjjd--- objs]# ls
addon autoconf.err Makefile nginx nginx. ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src 把objs目录下的nginx拷贝到nginx之前安装好的目录下 替换到原来的二进制文件

Nginx的基础配置管理的更多相关文章

  1. Linux架构之Nginx Web基础1

    第41章 Nginx Web基础入门 41.1 Nginx部署 41.1.1 Nginx的安装方式   源码编译 官方仓库 epel仓库 优点 规范 安装简单 安装简单   便于管理 配置易读   缺 ...

  2. nginx 的基础配置[转]

    nginx 的基础配置 分类: 工具软件2013-11-13 23:26 11人阅读 评论(0) 收藏 举报   目录(?)[-] 管理配置文件 全局配置 虚拟机server配置 location配置 ...

  3. nginx的基础应用

    nginx的基础应用 一.简介 今天我们将介绍一些nginx的简单应用,启动.停止nginx,重载nginx的配置,nginx配置文件的格式,如何配置nginx服务静态资源,如何配置nginx作为反向 ...

  4. nginx的基础应用(续)

    nginx的基础应用(续) 一.简介 上一篇文章我们介绍了nginx的基础应用,其中讲到了nginx作为代理服务器的使用,但是漏了一个重要的,也是使用非常普遍的特性--负载均衡.今天,我们将这段内容补 ...

  5. Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性

    简介 Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很 ...

  6. Nginx——1.基础知识

    Nginx——1.基础知识 作为高速.轻量.高性能等优点集于一身的服务器,Nginx在近些年迅速发展并不断扩大市场份额,甚至在最近其市场份额一举超过微软的IIS,跃身到第二位,仅次于Apache. 但 ...

  7. nginx实现基础web

    目录 nginx实现基础web 什么是lnmp lnmp架构如何工作 Nginx与Fast-CGO详细工作流程 LNMP环境准备 一,部署LNMP 1.使用nginx官方源 2.创建nginx用户 3 ...

  8. Nginx web基础入门

    目录 Nginx web基础入门 如何升级nginx或者添加功能 使用systemd管理nginx nginx相关配置文件 nginx的配置文件详解 日志格式 game日志记录实战 日志切割 手写虚拟 ...

  9. Nginx 常用基础模块

    目录 Nginx 常用基础模块 Nginx日志管理 nginx日志切割 Nginx目录索引 Nginx状态监控 Nginx访问控制 Nginx访问限制 Nginx 请求限制配置实战 Nginx Loc ...

随机推荐

  1. 仿迅雷播放器教程 -- duilib界面(13)

    经过了这么多篇文章的讲解,相信大家也对界面库有一定了解了,用一个新的界面库,肯定要对它进行全方位考察.鉴于公司目前所有的产品都是MFC做的,全部转换成duilib肯定不现实,并且公司的很多项目逻辑和界 ...

  2. 在linux中安装字体

    https://blog.csdn.net/wangxintong_1992/article/details/81194896

  3. PHP-CLI环境变量的设置和读取

    http://luokr.com/p/30 通常我们在维护PHP线上项目的时候,为了隔离配置和代码,会使用fastcgi_param的形式将环境变量定义在Nginx的配置文件中(Apache可以使用S ...

  4. 131、ThreadLocal (转载)

    http://blog.csdn.net/lufeng20/article/details/24314381 http://baike.baidu.com/link?url=7eL0qQm_5ULls ...

  5. C#设计模式--观察者模式(发布-订阅模式)

    0.C#设计模式--简单工厂模式 1.C#设计模式--工厂方法模式 2.C#设计模式--抽象工厂模式 3.C#设计模式--单例模式 4.C#设计模式--建造者模式 5.C#设计模式--原型模式 6.C ...

  6. TOP100summit:【分享实录-Microsoft】基于Kafka与Spark的实时大数据质量监控平台

    本篇文章内容来自2016年TOP100summit Microsoft资深产品经理邢国冬的案例分享.编辑:Cynthia 邢国冬(Tony Xing):Microsoft资深产品经理.负责微软应用与服 ...

  7. C# 调用存储过程 Sql Server存储过程 存储过程报错,程序中的try

    C#程序调用Sql Server存储过程,存储过程中报错情况,返回值... 0.SQL存储过程 USE [Opos] GO /****** Object: StoredProcedure [dbo]. ...

  8. tensorflow 计算均值和方差

    我们在处理矩阵数据时,需要用到数据的均值和方差,比如在batch normalization的时候. 那么,tensorflow中计算均值和方差的函数是:tf.nn.moments(x, axes) ...

  9. MySQL5.6启动报错The server quit without updating PID file

    Mysql启动报错如下: [root@db01 opt]# service mysqld start Starting MySQL.... ERROR! The server quit without ...

  10. 没有文件扩展js的脚本引擎

    没有文件扩展js的脚本引擎 没有文件扩展js的脚本引擎怎么解决_百度经验 https://jingyan.baidu.com/article/ff42efa93a7ad9c19e2202f0.html