一、简单介绍

由俄罗斯程序员IgorSysoev研发,2004年开源公布,特点是:内存cpu占用低,并发能力强,稳定,配置示例,反向代理;互联网企业 70%以上公司都在使用 nginx;

二、安装

1、下载地址

三、配置

1、配置结构(借用地址

  • 全局块

该部分配置主要影响Nginx全局,通常包括下面几个部分:

配置运行Nginx服务器用户(组),worker process数,Nginx进程PID存放路径,错误日志的存放路径,配置文件的引入;

 #运行用户,默认由nobody账号,可不设置
user nginx
#nginx进程,一般设置为和cpu核数一样
worker_processes ;
#错误日志存放目录
error_log /data1/logs/error.log;
#进程pid存放位置
pid /application/nginx/nginx.pid;
  • events块

该部分配置主要影响Nginx服务器与用户的网络连接,主要包括:

设置网络连接的序列化,是否允许同时接收多个网络连接,事件驱动模型的选择,最大连接数的配置;

#use是个事件模块指令,用来指定Nginx的工作模式。Nginx支持的工作模式有select、poll、kqueue、epoll、rtsig和/dev/poll。
#其中select和poll都是标准的工作模式,kqueue和epoll是高效的工作模式,不同的是epoll用在Linux平台上,而kqueue用在BSD系统中。对于Linux系统,epoll工作模式是首选。
use epoll; #worker_connections也是个事件模块指令,用于定义Nginx每个进程的最大连接数,默认是1024。
#最大客户端连接数由worker_processes和worker_connections决定,即Max_client=worker_processes*worker_connections。
worker_connections ;

http块

定义MIMI-Type,自定义服务日志,允许sendfile方式传输文件,连接超时时间,单连接请求数上限;

#include是个主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度。
include mime.types; #文件扩展名与类型映射表
#default_type属于HTTP核心模块指令,这里设定默认类型为二进制流
default_type application/octet-stream; #默认文件类型
#log_format是Nginx的HttpLog模块指令,用于指定Nginx日志的输出格式。
#设置日志模式
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#连接超时时间,单位是秒
keepalive_timeout 60;

除此之外,负载均衡的服务器配置也在这

#upstream表示负载服务器池,定义名字为backend_server的服务器池
upstream backend_server {
server 10.254.244.20: weight= max_fails= fail_timeout=30s;
server 10.254.242.40: weight= max_fails= fail_timeout=30s;
server 10.254.245.19: weight= max_fails= fail_timeout=30s;
server 10.254.243.39: weight= max_fails= fail_timeout=30s;
#设置由 fail_timeout 定义的时间段内连接该主机的失败次数,以此来断定 fail_timeout 定义的时间段内该主机是否可用。
#设置在指定时间内连接到主机的失败次数,超过该次数该主机被认为不可用。
#综上,这里是各个服务器权重一致,且在30s内尝试2次失败即认为主机不可用!
}
  • server块

配置网络监听,基于名称的虚拟主机配置,基于IP的虚拟主机配置;

 #server标志定义虚拟主机开始
#listen用于指定虚拟主机的服务端口;
listen ;#监听端口
#server_name用来指定IP地址或者域名,多个域名之间用空格分 开;
server_name www.abc.com abc.com;
#index用于设定访问的默认首页地址;
index index.html index.htm index.php; #首页排序
#root指令用于指定虚拟主机的网页根目录,这个目录可以是相对路径,也可以是绝对路径。
root /data0/abc; #站点根目录,即网站程序存放目录
####Charset用于 设置网页的默认编码格式。
charset gb2312;
####access_log用来指定此虚拟主机的访问日志存放路径,最后的main用于指定访问日志的输出格式。
access_log logs/www.ixdba.net.access.log main;
  • location块

location配置,请求根目录配置,更改location的URI,网站默认首页配置;

2、负载均衡的配置

 upstream abc_controll {
server 127.0.0.1:;
server 127.0.0.1:;
}
server {
listen ;
server_name www.abc.cn;
root /Users/scott/soft/apache-tomcat-9.0./webapps/;
index index;
access_log /Users/scott/soft/runlogs/nginx/abc_access.log main;
location / {
if ($request_filename ~* \.(gif|jpg|png|css|js|swf|flv|rar|zip|wmv)$)
{
expires 30d;
access_log off;
}
proxy_pass http://abc_controll;
}
error_page /50x.html;
location = /50x.html {
root /usr/local/www/nginx-dist;
}
}

3、杂项查阅

 #nginx进程,一般设置为和cpu核数一样
worker_processes ;
#错误日志存放目录
error_log /data1/logs/error.log crit;
#运行用户,默认即是nginx,可不设置
user nginx
#进程pid存放位置
pid /application/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process.
#最大文件打开数(连接),可设置为系统优化后的ulimit -HSn的结果
worker_rlimit_nofile ; cpu亲和力配置,让不同的进程使用不同的cpu worker_cpu_affinity ; #工作模式及连接数上限
events
{
use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
worker_connections ; #;单个后台worker process进程的最大并发链接数
}
###################################################
http
{ include mime.types; #文件扩展名与类型映射表
default_type application/octet-stream; #默认文件类型 #limit模块,可防范一定量的DDOS攻击
#用来存储session会话的状态,如下是为session分配一个名为one的10M的内存存储区,限制了每秒只接受一个ip的一次请求 1r/s
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;
include mime.types;
default_type application/octet-stream; #第三方模块lua防火墙
lua_need_request_body on;
#lua_shared_dict limit 50m;
lua_package_path "/application/nginx/conf/waf/?.lua";
init_by_lua_file "/application/nginx/conf/waf/init.lua";
access_by_lua_file "/application/nginx/conf/waf/access.lua"; #设定请求缓存
server_names_hash_bucket_size ;
client_header_buffer_size 512k;
large_client_header_buffers 512k;
client_max_body_size 100m; #隐藏响应header和错误通知中的版本号
server_tokens off;
#开启高效传输模式
sendfile on; -------------------------------------------------------------------------------------------------
#激活tcp_nopush参数可以允许把httpresponse header和文件的开始放在一个文件里发布,
积极的作用是减少网络报文段的数量
tcp_nopush on;
#激活tcp_nodelay,内核会等待将更多的字节组成一个数据包,从而提高I/O性能
tcp_nodelay on; #FastCGI相关参数:为了改善网站性能:减少资源占用,提高访问速度 fastcgi_connect_timeout ;
fastcgi_send_timeout ;
fastcgi_read_timeout ;
fastcgi_buffer_size 64k;
fastcgi_buffers 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k; ---------------------------------------------- #连接超时时间,单位是秒
keepalive_timeout ; #开启gzip压缩功能
gzip on;
#设置允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取。默认值是0,表示不管页面多大都进行压缩。建议设置成大于1K。如果小于1K可能会越压越大。
gzip_min_length 1k; #压缩缓冲区大小。表示申请4个单位为16K的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果。
gzip_buffers 16k; #压缩版本(默认1.1,前端为squid2.5时使用1.0)用于设置识别HTTP协议版本,默认是1.1,目前大部分浏览器已经支持GZIP解压,使用默认即可。
gzip_http_version 1.0; #压缩比率。用来指定GZIP压缩比,1压缩比最小,处理速度最快;9压缩比最大,传输速度快,但处理最慢,也比较消耗cpu资源。
gzip_comp_level ; #用来指定压缩的类型,“text/html”类型总是会被压缩
gzip_types text/plain application/x-javascript text/css application/xml;
#vary header支持。该选项可以让前端的缓存服务器缓存经过GZIP压缩的页面,例如用 Squid缓存经过Nginx压缩的数据。 gzip_vary off;
#开启ssi支持,默认是off
ssi on;
ssi_silent_errors on;
#设置日志模式
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for'; #反向代理负载均衡设定部分 #upstream表示负载服务器池,定义名字为backend_server的服务器池
upstream backend_server {
server 10.254.244.20: weight= max_fails= fail_timeout=30s;
server 10.254.242.40: weight= max_fails= fail_timeout=30s;
server 10.254.245.19: weight= max_fails= fail_timeout=30s;
server 10.254.243.39: weight= max_fails= fail_timeout=30s;
#设置由 fail_timeout 定义的时间段内连接该主机的失败次数,以此来断定 fail_timeout 定义的时间段内该主机是否可用。默认情况下这个数值设置为 1。零值的话禁用这个数量的尝试。 设置在指定时间内连接到主机的失败次数,超过该次数该主机被认为不可用。 #这里是在30s内尝试2次失败即认为主机不可用!
}
################### #基于域名的虚拟主机
server
{ #监听端口
listen ;
server_name www.abc.com abc.com;
index index.html index.htm index.php; #首页排序
root /data0/abc; #站点根目录,即网站程序存放目录
error_page /templates/kumi/phpcms/.html; #错误页面
#伪静态 将www.abc.com/list....html的文件转发到index.php。。。
#rewrite ^/list-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /index.php?m=content&c=index&a=lists&catid=$1&types=$2&country=$3&language=$4&age=$5&startDate=$6&typeLetter=$7&type=$8&page=$9 last;
#location 标签,根目录下的.svn目录禁止访问
location ~ /.svn/ {
deny all;
}
location ~ \.php$
{ #符合php扩展名的请求调度到fcgi server
fastcgi_pass 127.0.0.1:; #抛给本机的9000端口
fastcgi_index index.php; #设定动态首页
include fcgi.conf;
}
allow 219.237.222.30 ; #允许访问的ip
allow 219.237.222.31 ;
allow 219.237.222.32 ;
allow 219.237.222.33 ;
allow 219.237.222.34 ;
allow 219.237.222.35 ;
allow 219.237.222.61 ;
allow 219.237.222.28 ;
deny all; #禁止其他ip访问
}
location ~ ^/admin.php
{
location ~ \.php$
{
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
include fcgi.conf;
}
allow 219.237.222.30 ;
allow 219.237.222.31 ;
allow 219.237.222.32 ;
allow 219.237.222.33 ;
allow 219.237.222.34 ;
allow 219.237.222.35 ;
allow 219.237.222.61;
allow 219.237.222.28;
deny all;
} #将符合js,css文件的等设定expries缓存参数,要求浏览器缓存。 location~ .*\.(js|css)?$ { expires 30d; #客户端缓存上述js,css数据30天 } ##add by 20140321#######nginx防sql注入########## ###start####
if ( $query_string ~* ".*[\;'\<\>].*" ){
return ;
}
if ($query_string ~* ".*(insert|select|delete|update|count|\*|%|master|truncate|declare|\'|\;|and|or|\(|\)|exec).* ")
{
return ;
}
if ($request_uri ~* "(cost\()|(concat\()") {
return ;
}
if ($request_uri ~* "[+|(%20)]union[+|(%20)]") {
return ;
}
if ($request_uri ~* "[+|(%20)]and[+|(%20)]") {
return ;
}
if ($request_uri ~* "[+|(%20)]select[+|(%20)]") {
return ;
}
set $block_file_injections ;
if ($query_string ~ "[a-zA-Z0-9_]=(\.\.//?)+") {
set $block_file_injections ;
}
if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") {
set $block_file_injections ;
}
if ($block_file_injections = ) {
return ;
}
set $block_common_exploits ;
if ($query_string ~ "(<|%3C).*script.*(>|%3E)") {
set $block_common_exploits ;
}
if ($query_string ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})") {
set $block_common_exploits ;
}
if ($query_string ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") {
set $block_common_exploits ;
}
if ($query_string ~ "proc/self/environ") {
set $block_common_exploits ;
}
if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3D)") {
set $block_common_exploits ;
}
if ($query_string ~ "base64_(en|de)code\(.*\)") {
set $block_common_exploits ;
}
if ($block_common_exploits = ) {
return ;
}
set $block_spam ;
if ($query_string ~ "\b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)\b") {
set $block_spam ;
}
if ($query_string ~ "\b(erections|hoodia|huronriveracres|impotence|levitra|libido)\b") {
set $block_spam ;
}
if ($query_string ~ "\b(ambien|blue\spill|cialis|cocaine|ejaculation|erectile)\b") {
set $block_spam ;
}
if ($query_string ~ "\b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)\b") {
set $block_spam ;
}
if ($block_spam = ) {
return ;
}
set $block_user_agents ;
if ($http_user_agent ~ "Wget") {
set $block_user_agents ;
}
# Disable Akeeba Remote Control 2.5 and earlier
if ($http_user_agent ~ "Indy Library") {
set $block_user_agents ;
}
# Common bandwidth hoggers and hacking tools.
if ($http_user_agent ~ "libwww-perl") {
set $block_user_agents ;
}
if ($http_user_agent ~ "GetRight") {
set $block_user_agents ;
}
if ($http_user_agent ~ "GetWeb!") {
set $block_user_agents ;
}
if ($http_user_agent ~ "Go!Zilla") {
set $block_user_agents ;
}
if ($http_user_agent ~ "Download Demon") {
set $block_user_agents ;
}
if ($http_user_agent ~ "Go-Ahead-Got-It") {
set $block_user_agents ;
}
if ($http_user_agent ~ "TurnitinBot") {
set $block_user_agents ;
}
if ($http_user_agent ~ "GrabNet") {
set $block_user_agents ;
}
if ($block_user_agents = ) {
return ;
} ###end####
location ~ ^/list {
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 1d;
#proxy_cache_valid any 1d;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";
#proxy_ignore_headers Set-Cookie;
#proxy_hide_header Set-Cookie;
proxy_pass http://backend_server;
add_header Nginx-Cache "$upstream_cache_status from km"; expires 1d;
} access_log /data1/logs/abc.com.log access; #nginx访问日志
}
-----------------------ssl(https)相关------------------------------------ server {
  listen ; #监听端口
  server_name localhost;
  charset utf-; #gbk,utf-8,gb2312,gb18030 可以实现多种编码识别
  ssl on; #开启ssl
  ssl_certificate /ls/app/nginx/conf/mgmtxiangqiankeys/server.crt; #服务的证书
  ssl_certificate_key /ls/app/nginx/conf/mgmtxiangqiankeys/server.key; #服务端key
  ssl_client_certificate /ls/app/nginx/conf/mgmtxiangqiankeys/ca.crt; #客户端证书
  ssl_session_timeout 5m; #session超时时间
  ssl_verify_client on; # 开户客户端证书验证
  ssl_protocols SSLv2 SSLv3 TLSv1; #允许SSL协议
  ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; #加密算法
  ssl_prefer_server_ciphers on; #启动加密算法
  access_log /lw/logs/nginx/dataadmin.test.com.ssl.access.log access ; #日志格式及日志存放路径
  error_log /lw/logs/nginx/dataadmin.test.com.ssl.error.log; #错误日志存放路径 } -------------------------------------------------------------------------
}

四、测试

Nginx配置与使用的更多相关文章

  1. nginx配置反向代理或跳转出现400问题处理记录

    午休完上班后,同事说测试站点访问接口出现400 Bad Request  Request Header Or Cookie Too Large提示,心想还好是测试服务器出现问题,影响不大,不过也赶紧上 ...

  2. Windos环境用Nginx配置反向代理和负载均衡

    Windos环境用Nginx配置反向代理和负载均衡 引言:在前后端分离架构下,难免会遇到跨域问题.目前的解决方案大致有JSONP,反向代理,CORS这三种方式.JSONP兼容性良好,最大的缺点是只支持 ...

  3. Windows下Nginx配置SSL实现Https访问(包含证书生成)

    Vincent.李   Windows下Nginx配置SSL实现Https访问(包含证书生成) Windows下Nginx配置SSL实现Https访问(包含证书生成) 首先要说明为什么要实现https ...

  4. Nginx 配置简述

    不论是本地开发,还是远程到 Server 开发,还是给提供 demo 给人看效果,我们时常需要对 Nginx 做配置,Nginx 的配置项相当多,如果考虑性能配置起来会比较麻烦.不过,我们往往只是需要 ...

  5. Nginx配置详解

    序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...

  6. Nginx配置Https

    1.申请证书: https://console.qcloud.com/ssl?utm_source=yingyongbao&utm_medium=ssl&utm_campaign=qc ...

  7. nginx配置为windows服务中的坑

    网上搜索“nginx 配置为windows服务”,很容易搜索到使用windows server warpper来配置,于是按照网上的方法我从github上的链接下载了1.17版本,前面都很顺利,很容易 ...

  8. 【nginx配置】nginx做非80端口转发

    一个场景 最近在使用PHP重写一个使用JAVA写的项目,因为需要查看之前的项目,所以要在本地搭建一个Tomcat来跑JAVA的项目.搭建成功后,因为Tomcat监听的端口是8080,因此,访问的URL ...

  9. Apache、nginx配置的网站127.0.0.1可以正常访问,内外网的ip地址无法访问,谁的锅?

    最近做开发,发现一个比较尴尬的问题.因为我是一个web开发者,经常要用到Apache或者nginx等服务器软件,经过我测试发现,只要我打开了adsafe,我便不能通过ip地址访问我本地的网站了,比如我 ...

  10. nginx配置301重定向

    1. 简介 301重定向可以传递权重,相比其他重定向,只有301是最正式的,不会被搜索引擎判断为作弊 2. 栗子 savokiss.com 301到 savokiss.me 3. nginx默认配置方 ...

随机推荐

  1. 学到了林海峰,武沛齐讲的Day51 django+数据库

    连不上,通过这一步解决 搞死了..辛苦但觉得值得 刷数据库 出问题 IDEA关联MySQL报错:Server returns invalid timezone. Go to ‘Advanced’ ta ...

  2. C# 选择文件夹 选择文件

    选择文件 //选择文件 OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以选择多个文件 ...

  3. Ajax异步传值总结

    Ajax异步传值 将数据从前台传向后台: 1:通过get方式,将参数在链接中,配合“?”进行传值. 实例: //前台传值方法 //触发该方法调用ajax function testAjax(yourD ...

  4. poj 2566 Bound Found 尺取法 变形

    Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2277   Accepted: 703   Spec ...

  5. 文章翻译:ABP如何在EF core中添加数据过滤器

    原文地址:https://aspnetboilerplate.com/Pages/Documents/Articles%5CHow-To%5Cadd-custom-data-filter-ef-cor ...

  6. Jmeter(一) 安装

    一.检查JDK版本 执行cmd > java -version 查看本机JDK版本,JDK版本不能低于1.6 二.软件下载 登录Jmeter官网:https://jmeter.apache.or ...

  7. B. Chocolates

    B. Chocolates time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  8. Python中的不可变对象类型与可变对象类型

    https://blog.csdn.net/answer3lin/article/details/86430074 其实各个标准资料中没有说明Python有值类型和引用类型的分类,这个分类一般是C++ ...

  9. vue-cli的基础构造

    1,项目目录 2, bulid 下文件及目录 3,config下文件及目录 接下来说说vue-cli项目中页面相关的主要文件^o^ 首先是index.html: 说明:一般只定义一个空的根节点,在ma ...

  10. Spring Data JPA的Respository接口中查询方法