Nginx的基础配置管理
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配置段
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的基础配置管理的更多相关文章
- Linux架构之Nginx Web基础1
第41章 Nginx Web基础入门 41.1 Nginx部署 41.1.1 Nginx的安装方式 源码编译 官方仓库 epel仓库 优点 规范 安装简单 安装简单 便于管理 配置易读 缺 ...
- nginx 的基础配置[转]
nginx 的基础配置 分类: 工具软件2013-11-13 23:26 11人阅读 评论(0) 收藏 举报 目录(?)[-] 管理配置文件 全局配置 虚拟机server配置 location配置 ...
- nginx的基础应用
nginx的基础应用 一.简介 今天我们将介绍一些nginx的简单应用,启动.停止nginx,重载nginx的配置,nginx配置文件的格式,如何配置nginx服务静态资源,如何配置nginx作为反向 ...
- nginx的基础应用(续)
nginx的基础应用(续) 一.简介 上一篇文章我们介绍了nginx的基础应用,其中讲到了nginx作为代理服务器的使用,但是漏了一个重要的,也是使用非常普遍的特性--负载均衡.今天,我们将这段内容补 ...
- Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性
简介 Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很 ...
- Nginx——1.基础知识
Nginx——1.基础知识 作为高速.轻量.高性能等优点集于一身的服务器,Nginx在近些年迅速发展并不断扩大市场份额,甚至在最近其市场份额一举超过微软的IIS,跃身到第二位,仅次于Apache. 但 ...
- nginx实现基础web
目录 nginx实现基础web 什么是lnmp lnmp架构如何工作 Nginx与Fast-CGO详细工作流程 LNMP环境准备 一,部署LNMP 1.使用nginx官方源 2.创建nginx用户 3 ...
- Nginx web基础入门
目录 Nginx web基础入门 如何升级nginx或者添加功能 使用systemd管理nginx nginx相关配置文件 nginx的配置文件详解 日志格式 game日志记录实战 日志切割 手写虚拟 ...
- Nginx 常用基础模块
目录 Nginx 常用基础模块 Nginx日志管理 nginx日志切割 Nginx目录索引 Nginx状态监控 Nginx访问控制 Nginx访问限制 Nginx 请求限制配置实战 Nginx Loc ...
随机推荐
- [Node.js] 04 - Event and Callback
回调函数 回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,Node 所有 API 都支持回调函数. 异步读取文件的回调函数: var fs = require("fs&quo ...
- 给 Advice 传递参数
参数绑定是在下面这个方法中做的:org.springframework.aop.aspectj.AbstractAspectJAdvice#invokeAdviceMethod(JoinPoint j ...
- 前端和后端的数据交互(jquery ajax+python flask+mysql)
上web课的时候老师布置的一个实验,要求省市连动,基本要求如下: 1.用select选中一个省份. 2.省份数据传送到服务器,服务器从数据库中搜索对应城市信息. 3.将城市信息返回客户,客户用sele ...
- 10.22crm
2018-10-23 08:28:41 由于昨晚上断网,所以今天早上补更一下 越努力,越幸运!永远不要高估自己! crm老师就讲三四天,还有明后两天! 主要是讲一些基本使用,自定义配置陪一些东西! 主 ...
- python数据类型之pandas—DataFrame
DataFrame定义: DataFrame是pandas的两个主要数据结构之一,另一个是Series —一个表格型的数据结构 —含有一组有序的列 —大致可看成共享同一个index的Series集合 ...
- Thinkphp 中的自动验证 上一篇有例子
说明,只要定义了Model,在任何地方调用,都会进行验证.很方便. 必须是D方法,才会有效.M方法不会触发自动验证. 说明:这里没练习静态自动验证:如果用到静态验证首先自定义一个控制器,再在Model ...
- Javascript扩展Date的prototype实现时间format函数 2017-06-29T09:10:00.000Z日期转换
/*时间格式化 公用方法*/ Date.prototype.format = function(fmt) { // var o = { "M+": this.getMonth() ...
- css学习_写法规范、选择器
1.css(层叠样式表)样式主要目的(让页面更好看些) css尽量不要写内联样式,保证结构和样式分离原则: html专门负责结构,css专门负责样式. 2.css写法规范 选择器 { 属性 :值 : ...
- BZOJ 1002 - 轮状病毒 - [基尔霍夫矩阵(待补)+高精度]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1002 Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生 ...
- [No0000175]maven常用命令集合(收藏大全)
抽了点时间,整理了一些maven常用命令参数,以便参考:参考了maven官网和网上其他一些maven追随者的文件,不在此一一列举,但表示感谢! mvn命令参数 mvn -v, --version 显示 ...