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. [IR] Time and Space Efficiencies Analysis of Full-Text Index Techniques

    文章阅读:全文索引技术时空效率分析 LIU Xiao-ZhuPENG Zhi-Yong 根据全文索引实现技术的不同,将其分为三大类: 索引技术 (倒排文件.签名文件 .后缀树与后缀数组) 压缩与索引混 ...

  2. CentOS7 设置软件镜像源

    Linux拥有众多的发行版,每个发行版都有提供镜像,但是,在国内这些镜像的下载速度参差不齐,偶尔会有更新失败的情况. 网上众多推荐的镜像源为163的镜像,笔者通过一段时间的试用,发现某些小众带宽访问1 ...

  3. image-set实现Retina屏幕下图片显示详细介绍

    支持image-set:如果你的浏览器支持image-sete,而且是普通显屏下,此时浏览器会选择image-set中的@1x背景图像: Retina屏幕下的image-set:如果你的浏览器支持im ...

  4. [原]openstack-kilo--issue(十)ERROR: openstack Unable to establish connection to http://controller:35357/v3/auth/tokens

    ====环境== openstack :kilo CentOS : 7 ====问题=== 在没有关vm的情况下,重启了controller. 问题一: 在使用nova service-list 或者 ...

  5. 跟bWAPP学WEB安全(PHP代码)--XPath注入

    XML/Xpath注入 看了下,A2里面是认证与会话管理的破坏或称之为绕过,没有特别要写的,很多就是小问题,可能会将这类问题放在最后写一下.一篇博客,这里还是更多的着重在能够获取信息或者服务器权限的漏 ...

  6. jmeter中split分隔字符

    jmeter中自带了split函数,但是,注意但是,不能使用"-",","等等符合,否则会报错. 没办法,转投入beanshell的怀抱,使用split,发现& ...

  7. 同一个tomcat使用不同http端口配置多个web项目

    1.复制 conf/server.xml下的 复制粘贴新的一个Service元素下的所有内容,并修改name为Catalina2,<Service name="Catalina&quo ...

  8. Hadoop学习之pig

    首先明确pig是解决什么问题而出现的,pig是为了简化mapreduce编程而设计的,并且有自己的一套脚本语言.其基本由命令和操作符来定义的,如load,store,它的功能很明确,用来大规模处理数据 ...

  9. B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表

    You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...

  10. linux 下实用软件工具推荐

    截图:Deepin-Screenshot 音乐:deepin-music netease-music 绘图工具:Draw.io Desktop (chrome extension) / www.pro ...