一、nginx简介

1.nginx的发展

  Nginx是俄罗斯人编写的一款高性能HTTP和反向代理服务器。Nginx能够选择高效的epoll(Linux2.6内核)、kqueue(FreeBSD)、eventport(Solaris 10)作为网络I/O模型,再高连接并发的场景下,Nginx是Apache服务器非常不错的替代品,它能够支持50000个并发连接数的响应,而CPU、内存等系统资源消耗却非常低,运行非常稳定。

2.为什么选择Nginx

2.1 它可以高并发连接

  官方测试Nginx可以支持5w并发连接,在实际生产环境中可以支持2~4w并发连接数。这得益于Nginx使用了最新的epoll和kqueue网络I/O模型,而Apache则使用的老的select模型。

2.2 内存消耗少

2.3 成本低廉

3.Nginx和Apache、Lighttpd的综合对比

二、Nginx的基本配置和优化

#使用的用户和组
#user nobody;
#指定工作衍生进程数(一般等于CPU的总核数或总核数的两倍,例如两个4核CPU,则总核数为8)
worker_processes 1; #错误日志存放路径
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #指定PID存放路径
#pid logs/nginx.pid; events {
#允许的连接数
worker_connections 1024;
} 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;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; include https_params.conf; server {
listen 8888;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} location /test {
proxy_pass https://www.baidu.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Server-IP $server_name;
proxy_set_header X-Server-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme; proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 300;
} #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;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }

上面是Nginx的nginx.conf配置文件,可见配置文件的构成如下:

....
events {
....
}
....
http {
....
server {
....
}
server {
....
}
....
}

1.Nginx虚拟主机配置

1.1 什么是虚拟主机

  其实就是把一台运行在互联网上的服务器划分成多个“虚拟”的服务器,并且每一台虚拟主机都具有独立的域名和完整的Internet服务器功能。同一台服务器上的不同虚拟主机是各自独立的,可由客户自行管理。不过一台服务器只可以支持一定数量的虚拟主机,如果超出这个数量,那么客户在使用时将会发现性能急速降低。从网站访问者来看,每一台虚拟主机和一台独立的主机完全一样。如下是一个虚拟主机的代码:

server {
listen 8000;
server_name somename alias another.alias; location / {
root html;
index index.html index.htm;
}
}

1.2 配置基于IP的虚拟主机

  Linux、FreeBSD操作系统都允许添加ip别名。IP别名背后的概念很简单:可以在一块物理网卡上绑定多个ip地址。这样就可以在单一网卡的同一个服务器上运行多个基于ip的虚拟主机,nginx多ip虚拟主机配置如下:

http{
#第一个虚拟主机
server {
listen 192.168.1.1:8000;
server_name somename alias another.alias; location / {
root html;
index index.html index.htm;
}
}
#第二个虚拟主机
server {
listen 192.168.1.2:8000;
server_name somename alias another.alias; location / {
root html;
index index.html index.htm;
}
}
}

1.3 配置基于多域名的虚拟主机

  基于域名的虚拟主机是最常见的虚拟主机。只需配置你的DNS服务器,将每个主机名映射到正确的ip地址,然后配置Nginx服务器,令其识别不同的主机名就可以了。Nginx配置如下:

http{
#第一个虚拟主机
server {
listen 8000;
server_name www.baidu.com; location / {
root html;
index index.html index.htm;
}
}
#第二个虚拟主机
server {
listen 8000;
server_name www.weibo.com; location / {
root html;
index index.html index.htm;
}
}
}

nginx简介&nginx基本配置和优化的更多相关文章

  1. Nginx简介与基础配置

    何为Nginx? Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.最初是为了解决C10k的问题,由Igor ...

  2. nginx简介与配置

    nginx简介 nginx(发音同engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行. nginx由俄罗斯的程序 ...

  3. Nginx简介及配置实用

    Nginx简介 Nginx是一个高性能的HTTP和反向代理服务器: 支持的操作系统众多,windows.linux. MacOS X: 可实现负载均衡: Rewrite功能强大: 电商架构大部分都采用 ...

  4. nginx反向代理的配置优化

    作者:守住每一天 blog:liuyu.blog.51cto.combbs:bbs.linuxtone.orgmsn:liuyubj520#hotmail.comemail:liuyu105#gmai ...

  5. Nginx网络架构实战学习笔记(一):Nginx简介、安装、信号控制、nginx虚拟主机配置、日志管理、location 语法、Rewrite语法详解

    文章目录 nginx简介 nginx安装 nginx信号控制 nginx虚拟主机配置 日志管理 location 语法 精准匹配的一般匹配 正则匹配 总结 Rewrite语法详解 nginx简介 Ng ...

  6. Nginx高性能服务器安装、配置、运维 (1) —— Nginx简介

    一.Nginx 简介 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,同时也是一个 IMAP/POP3/SMTP 代理服务器. Nginx特点 ...

  7. nginx安装升级及配置详解

    1.简介 2.安装配置 3.配置文件介绍 4.启动.停止.平滑重启.升级 一.Nginx简介 Nginx(engine x)是俄罗斯人Igor Sysoev编写的一款高性能的http和反向代理服务器. ...

  8. centos6 Nginx+Tomcat负载均衡配置

    一.Nginx简介 Nginx是一个web服务器也可以用来做负载均衡及反向代理使用,目前使用最多的就是负载均衡,具体简介我就不介绍了百度一下有很多,下面直接进入安装步骤 二.Nginx安装 1.下载N ...

  9. Nginx简介及配置文件详解

    http://blog.csdn.net/hzsunshine/article/details/63687054 一 Nginx简介    Nginx是一款开源代码的高性能HTTP服务器和反向代理服务 ...

随机推荐

  1. 实测Tengine开源的Dubbo功能

    本文已收录 https://github.com/lkxiaolou/lkxiaolou 欢迎star. 搜索关注微信公众号"捉虫大师",后端技术分享,架构设计.性能优化.源码阅读 ...

  2. pytest--fixture基本使用(主要用来进行测试环境的初始化和清理,fixture中的params参数还可以用来进行参数化)

    fixture fixture修饰器来标记固定的工厂函数,在其他函数,模块,类或整个工程调用它时会被激活并优先执行,通常会被用于完成预置处理和重复操作. 方法: fixture(scope=" ...

  3. k8s搭建链路监控:skywalking

    skywalking架构及简介 官网:https://github.com/apache/skywalking 简介 Java, .NET Core, NodeJS, PHP, and Python ...

  4. Go1.14版本vendor和gomodule冲突问题

    Go1.14版本vendor和gomodule冲突问题 go1.14版本使用go mod tidy构建依赖时会出现问题(见链接), 这个问题在go1.12版本是不会出现的. https://githu ...

  5. 数据可视化地图制作教程,这个免费BI软件轻松搞定

    ​数据可视化地图制作教程 现在做数据分析基本上离不开数据可视化,在大量的数据中,有很大一部分数据都与地理信息相关,因此,在数据可视化中,可视化地图是非常重要的一部分.无论是新闻报道,还是商业分析报告, ...

  6. 思迈特软件Smartbi光鲜亮丽的背后是什么在支撑?

    思迈特软件Smartbi是国内知名BI厂商,自2011年成立以来就以提升和挖掘客户的价值为使命,致力于为客户提供一站式商业智能平台和BI解决方案,发展到如今已经获得了来自国家.地方政府.国内外权威分析 ...

  7. windev中自定义选定列的使用和注意事项

    windev系统默认的多选,需要使用Ctrl+或者Shift+来点选,使用并不方便,所以我们一般在首列增加checkbox列,并在行头增加一个checkbox控制,作为全选使用.使用这个方法时,有几个 ...

  8. windows消息机制框架原理【简单版本】

    windows消息机制框架原理 结合两张图理解 窗口和窗口类 Windows UI 应用程序 (e) 具有一个主线程 (g).一个或多个窗口 (a) 和一个或多个子线程 (k) [工作线程或 UI 线 ...

  9. 【基础知识】CPU 指令执行的五个阶段,cpu就是用来执行指令的

    IF(Instruction fetch) 取指:从 Instruction-Memory 中读取指令,并在下一个时钟上升沿到来时把指令送到 ID 级的指令缓冲器 id_ir 中.该级控制信号决定下一 ...

  10. el-dialog设置为点击弹窗以外的区域不自动关闭弹窗

    两种方法:单个设置或者全局设置 第一种:(单个设置) 在el-dialog标签中添加:close-on-click-modal="false"即可 <el-dialog ti ...