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. /usr/bin/ld: cannot find -lncurses是咋回事?

    你的系統是32位的還是64位的? 如果是32位的就用:sudo apt-get install libncurses5-dev 如果是64位的,就用:sudo apt-get install lib3 ...

  2. Hadoop初期学习和集群搭建

    留给我学习hadoop的时间不多了,要提高效率,用上以前学的东西.hadoop要注重实战,把概念和原理弄清楚,之前看过一些spark,感觉都是一些小细节,对于理解hadoop没什么帮助.多看看资料,把 ...

  3. mysql中的schema 等价于database,相当于一个数据库

    MySQL 中 Schema 等价于 数据库. mysql> SELECT -> SCHEMA_NAME, -> DEFAULT_CHARACTER_SET_NAME, -> ...

  4. mapper.xml

    #{}如果是字符串就不用加引号,否则报错,${}也一样,不是占位符需要加

  5. scokte tcp/ip

    import scoket# 服务端 server = socket.socket() ip_port = ("127.0.0.1",8001) server.bind(ip_po ...

  6. Luogu 1494 - 小Z的袜子 - [莫队算法模板题][分块]

    题目链接:https://www.luogu.org/problemnew/show/P1494 题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天 ...

  7. [No0000108]Git1/9-Git简介与入门

    Git是什么? Git是目前世界上最先进的分布式版本控制系统(没有之一). Git有什么特点?简单来说就是:高端大气上档次! 那什么是版本控制系统? 如果你用Microsoft Word写过长篇大论, ...

  8. Drying POJ - 3104 二分 最优

    题意:有N件湿的衣服,一台烘干机.每件衣服有一个湿度值.每秒会减一,如果用烘干机,每秒会减k.问最少多久可以晒完. 题解:二分.首先时间越长越容易晒完. 其次判定函数可以这样给出:对于答案 X,每一个 ...

  9. 查看dsym的报错信息(友盟)

    //搜索显示报错信息的命令在终端直接运行(来源老版提示) export dSYMPath="$(find ~/Library/Developer/Xcode -iname '*.dSYM' ...

  10. 关于nginx重新编译

    nginx安装成功后,发现有一些其他模块没有编译进去,或者想额外添加一些模块,这时候就要重新编译nginx. 首先,查看之前编译的一些参数,比如: 1 2 3 4 5 [root@lmode ngin ...