博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
超简单nginx配置从入门到入土
阅读量:3960 次
发布时间:2019-05-24

本文共 13159 字,大约阅读时间需要 43 分钟。

文章目录

nginx 常见命令

nginx -s reload 服务不间断运行

nginx -t 语法检测
server nginx start --> /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx -s stop

配置文件

# For more information on configuration, see:#   * Official English Documentation: http://nginx.org/en/docs/#   * Official Russian Documentation: http://nginx.org/ru/docs/### 核心模块user root; #配置用户或者组worker_processes auto;#允许生成的进程数,默认为1error_log /var/log/nginx/error.log;  #错误日志路径,级别pid /run/nginx.pid;#指定 nginx 进程运行文件存放地址# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.include /usr/share/nginx/modules/*.conf;events {
#accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on #multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off #事件驱动模型select|poll|kqueue|epoll|resig #poll是多路复用IO(I/O Multiplexing)中的一种方式, #仅用于linux2.6以上内核,可以大大提高nginx的性能 use epoll; worker_connections 1024; #最大连接数,默认为512 #worker_cpu_affinity cpu亲和性 #worker_rlimit_nofile work进程最大打开文件数限制 open file too many # 并发总数是 worker_processes 和 worker_connections 的乘积 # 即 max_clients = worker_processes * worker_connections # 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4 为什么 # 为什么上面反向代理要除以4,应该说是一个经验值 # 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000 # worker_connections 值的设置跟物理内存大小有关 # 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数 # 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右 # 我们来看看360M内存的VPS可以打开的文件句柄数是多少: # $ cat /proc/sys/fs/file-max # 输出 34336 # 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内 # 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置 # 使得并发总数小于操作系统可以打开的最大文件数目 # 其实质也就是根据主机的物理CPU和内存进行配置 # 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。 # ulimit -SHn 65535}### http 模块http {
#设定日志格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #$remote_addr #记录访问网站的客户端地址#$remote_user #远程客户端用户名#$time_local #记录访问时间与时区#$request #用户的http请求起始行信息#$status #http状态码,记录请求返回的状态码,例如:200、301、404等#$body_bytes_sent #服务器发送给客户端的响应body字节数#$http_referer #记录此次请求是从哪个连接访问过来的,可以根据该参数进行防盗链设置。#$http_user_agent #记录客户端访问信息,例如:浏览器、手机客户端等#$http_x_forwarded_for #当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置 access_log /var/log/nginx/access.log main; #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件, #对于普通应用,必须设为 on, #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off, #以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfile on; tcp_nopush on; tcp_nodelay on; #连接超时时间 #keepalive_timeout 0; keepalive_timeout 65;#连接超时时间,默认为75s,可以在http,server,location块 types_hash_max_size 2048; #设定mime类型,类型由mime.type文件定义 include /etc/nginx/mime.types;#文件扩展名与文件类型映射表 default_type application/octet-stream;#默认文件类型,默认为text/plain #access_log off; #取消服务日志 #sendfile on; #允许 sendfile 方式传输文件,默认为off,可以在http块,server块,location块 #sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限 #开启gzip压缩 gzip on; gzip_disable "MSIE [1-6]."; #设定请求缓冲 client_header_buffer_size 128k; large_client_header_buffers 4 128k; # 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 {
#keepalive_requests 120; #单连接请求上限次数 listen 80 default_server;#监听端口,默认80 listen [::]:80 default_server;#服务域名,可用正则匹配 server_name _; root /root/recipe; #定义服务器的默认网站根目录位置 # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; #设定本虚拟主机的访问日志 access_log logs/nginx.access.log main; location / {
proxy_pass http://127.0.0.1:8080; #让后端的django知道最前面的客户机的信息(请求的url和ip地址) proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #root /usr/share/nginx/html; #index test.html; #定义首页索引文件的名称 } ~ 波浪线表示执行一个正则匹配,区分大小写~* 表示执行一个正则匹配,不区分大小写^~ 表示普通字符匹配,不是正则匹配。如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录= 进行普通字符精确匹配@ 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files error_page 404 @jump_page; location @jump_page {
return 200 "404 error test"; } #status统计 location /ngx_status {
stub_status on; access_log off; } location /static {
alias /root/recipe/allstatic; } error_page 404 /404.html; location = /40x.html {
} # 定义错误提示页面 error_page 500 502 503 504 /50x.html; location = /50x.html {
} #静态文件,nginx自己处理 location ~ ^/(images|javascript|js|css|flash|media|static)/ {
#过期30天,静态文件不怎么更新,过期可以设大一点, #如果频繁更新,则可以设置得小一点。 expires 30d; } #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置. location ~ .php$ {
fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #禁止访问 .htxxx 文件 location ~ /.ht {
deny all; } }# Settings for a TLS enabled server.## server {
# listen 443 ssl http2 default_server;# listen [::]:443 ssl http2 default_server;# server_name _;# root /usr/share/nginx/html;## ssl_certificate "/etc/pki/nginx/server.crt";# ssl_certificate_key "/etc/pki/nginx/private/server.key";# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 10m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;## # 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 {
# }# }}

启用nginx status状态详解

#在默认主机里面加上location或者你希望能访问到的主机里面。location /ngx_status {
stub_status on; access_log off; }curl http://127.0.0.1/ngx_statusActive connections: 11921 server accepts handled requests 11989 11989 11991Reading: 0 Writing: 7 Waiting: 42 #nginx status详解active connections – 活跃的连接数量server accepts handled requests — 总共处理了11989个连接 , 成功创建11989次握手, 总共处理了11991个请求reading — 读取客户端的连接数.writing — 响应数据到客户端的数量waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.

配置项解析

worker_processes#worker_processes用来设置 Nginx 服务的进程数。该值推荐使用 CPU 内核数。worker_cpu_affinity*#worker_cpu_affinity用来为每个进程分配CPU的工作内核,参数有多个二进制值表示,每一组代表一个进程,每组中的每一位代表该进程使用CPU的情况,1代表使用,0代表不使用。所以我们使用 worker_cpu_affinity0001001001001000;来让进程分别绑定不同的核上。默认情况下worker进程不绑定在任何一个CPU上。worker_rlimit_nofile#设置毎个进程的最大文件打开数。如果不设的话上限就是系统的 ulimit–n的数字,一般为65535。worker_connections#设置一个进程理论允许的最大连接数,理论上越大越好,但不可以超过 worker_rlimit_nofile的值。use epoll#设置事件驱动模型使用 epoll。epoll 是 Nginx 支持的高性能事件驱动库之一。是公认的非 常优秀的事件驱动模型。accept_mutex off#关闭网络连接序列化,当其设置为开启的时候,将会对多个 Nginx 进程接受连接进行序列化,防止多个进程对连接的争抢。当服务器连接数不多时,开启这个参数会让负载有一定程度的降低。但是当服务器的吞吐量很大时,为了效率,请关闭这个参数;并且关闭这个参数的时候也可以让请求在多个 worker 间的分配更均衡。所以我们设置 accept_mutex off;。multi_accept on#设置一个进程可同时接受多个网络连接。Sendfile on#Sendfile 是 Linux2.0 以后的推出的一个系统调用,它能简化网络传输过程中的步骤,提高服务器性能。不用 sendfile 的传统网络传输过程:硬盘 >> kernel buffer >> user buffer >> kernel socket buffer >> 协议栈用 sendfile来进行网络传输的过程:硬盘 >> kernel buffer (快速拷贝到 kernelsocket buffer) >>协议栈tcp_nopush on;#设置数据包会累积一下再一起传输,可以提高一些传输效率。tcp_nopush必须和 sendfile 搭配使用。tcp_nodelay on;#小的数据包不等待直接传输。默认为on。看上去是和 tcp_nopush相反的功能,但是两边都为 on 时 nginx 也可以平衡这两个功能的使用。keepalive_timeout#HTTP 连接的持续时间。设的太长会使无用的线程变的太多。这个根据服务器访问数量、处理速度以及网络状况方面考虑。send_timeout#设置 Nginx 服务器响应客户端的超时时间,这个超时时间只针对两个客户端和服务器建立连接后,某次活动之间的时间,如果这个时间后,客户端没有任何活动,Nginx 服务器将关闭连接。gzip on#启用 gzip,对响应数据进行在线实时压缩,减少数据传输量。gzip_disable "msie6"#Nginx服务器在响应这些种类的客户端请求时,不使用 Gzip 功能缓存应用数据, gzip_disable“msie6”对IE6浏览器的数据不进行 GZIP 压缩。

其他

http 配置里有 location这一项,它是用来根据请求中的 uri 来为其匹配相应的处理规则。location 查找规则location = / {
# 精确匹配 / ,主机名后面不能带任何字符串[ config A ]}location / {
# 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求# 但是正则和最长字符串会优先匹配[ config B ]}location /documents/ {
# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条[ config C ]}location ~ /documents/Abc {
# 匹配任何以 /documents/Abc 开头的地址,匹配符合以后,还要继续往下搜索# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条[ config CC ]}location ^~ /images/ {
# 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条[ config D ]}location ~* .(gif|jpg|jpeg)$ {
# 匹配所有以 gif,jpg或jpeg 结尾的请求# 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则[ config E ]}location /images/ {
# 字符匹配到 /images/,继续往下,会发现 ^~ 存在[ config F ]}location /images/abc {
# 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在# F与G的放置顺序是没有关系的[ config G ]}location ~ /images/abc/ {
# 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用[ config H ]}正则查找优先级从高到低依次如下:“ = ” 开头表示精确匹配,如 A 中只匹配根目录结尾的请求,后面不能带任何字符串。“ ^~ ” 开头表示uri以某个常规字符串开头,不是正则匹配。“ ~ ” 开头表示区分大小写的正则匹配。“ ~* ”开头表示不区分大小写的正则匹配。“ / ” 通用匹配, 如果没有其它匹配,任何请求都会匹配到。

负载均衡配置(反向代理)

在这里插入图片描述

Nginx 的负载均衡需要用到 upstream模块,可通过以下配置来实现:

upstream test-upstream {
ip_hash; # 使用 ip_hash 算法分配 server 192.168.1.1; # 要分配的 ip server 192.168.1.2 max_fails=1 fail_timeout=10s;}server {
location / {
proxy_pass http://test-upstream; }}

虚拟主机配置

基于域名的虚拟主机
$vim /etc/nginx/nginx.confuser nginx;worker_processes auto;error_log /var/log/nginx/error.log;pid /run/nginx.pid;events {
worker_connections 1024;}http {
include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server {
listen 80; server_name www.luo.com; location / {
root /usr/share/nginx/html/; index index.html index.htm; } } server {
listen 80; server_name bbs.luo.com; location / {
root /usr/share/nginx/bbs/; index index.html index.htm; } }}$echo www in Nginx > /usr/share/nginx/html/index.html $echo bbs in Nginx > /usr/share/nginx/bbs/index.html

在window上做好hosts解析后,测试如下:

在这里插入图片描述

基于端口的虚拟主机

在这里插入图片描述

mkdir  /usr/share/nginx/8080echo port 8080 in Nginx >  /usr/share/nginx/8080/index.htmlnginx -s reload

在这里插入图片描述

基于IP的虚拟主机

基于IP的虚拟主机用得比较少。当主机拥有多个IP时,修改server区块的listen ip:port即可。

https配置

server {
listen 2443 ssl; server_name www.xxxxx.com:2443; ssl on; ssl_certificate cert/cert.crt; ssl_certificate_key cert/cert.key; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; proxy_redirect http:// $scheme://; port_in_redirect on; location ~ ^/yst-orp {
proxy_pass http://192.168.1.23:8081; proxy_set_header Host $host:$server_port; 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_redirect参数;这个参数主要用来改从被代理服务器传来的应答头中的"Location"和"Refresh"字段。配置语法为:proxy_redirect [ default|off|redirect replacement ]默认的值为:proxy_redirect default 可使用的标签:http,server,location在此处配置为proxy_redirect http:// $scheme://;的作用是将从tomcat中返回的http修改为https。官方文档地址:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect port_in_redirect参数;这个参数的作用是启用或禁用在由nginx发布的绝对重定向中指定端口。配置语法:port_in_redirect [off|on]默认值:port_in_redirect on可使用此配置的标签:http,server,location此处配置为port_in_redirect on,作用是在nginx反向代理跳转到tomcat时将跳转到的端口替换为nginx监听的端口。官方文档地址:http://nginx.org/en/docs/http/ngx_http_core_module.html#port_in_redirect proxy_set_header参数这个参数的作用是允许重新定义或者添加发往后端服务器的请求头。value可以包含文本、变量或者它们的组合。 当且仅当当前配置级别中没有定义proxy_set_header指令时,会从上面的级别继承配置。 默认情况下,只有两个请求头会被重新定义:proxy_set_header Host $proxy_host;proxy_set_header Connection close;配置语法格式:port_in_redirect field value;可使用的标签:http,server,location此处设置为proxy_set_header Host $host:$server_port;的作用是将请求的报文的头部的客户端的ip更改为当前作为反向代理的nginx的监听的ip及端口,这样后端的tomcat处理完请求时返回给nginx,再由nginx代理将结果返回给客户端。不然,后端服务处理完请求会直接返回到客户端,这样会导致客户端请求的https跳转为http,以及请求超时。官方文档地址:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header绑定域名,通过域名访问配置完成后,在本地通过nginx配置的https可以正常访问(配置了https,通过ip加端口的方式访问时提示安全性问题是正常的),但是通过对外提供的域名加端口不能正常访问。这是由于nginx配置中proxy_set_header参数的设置所引起的,proxy_set_header设置为Host $host:$server_port,这样导致将客户端请求报文头部的源地址修改为nginx本地监听地址加端口即:(192.168.1.25):2443,,在内外端口不一致时对外将请求结果返回给客户端时目标地址将变为:域名(www.xxxxx.com):2443,这样导致客户通过域名访问时输入的地址www.xxxxx.com:1234变为www.xxxxx.com:2443。解决这个问题只需更改配置(修改匹配值proxy_set_header Host $host:$server_port;)为:proxy_set_header Host www.xxxxx.com:1234;这样设置的作用就是,服务端处理完请求,将处理结果原路返回,最终由www.xxxxx.com:1243代理服务端发送给客户端。这样设置后本地直接通过nginx主机访问时也会跳转为域名地址。注意:在通过域名加端口的方式测试时,nginx对外映射的地址和自己测试的终端的出口地址不能为同一公网ip,否则。本地通过域名测试时无法访问。

转载地址:http://smezi.baihongyu.com/

你可能感兴趣的文章
autoit3 ie.au3 函数之——_IE_VersionInfo
查看>>
autoit3 ie.au3 函数之——_IEAction
查看>>
autoit3 ie.au3 函数之——_IEGetObjById、_IEGetObjByName
查看>>
autoit3 ie.au3 函数之——_IEAttach
查看>>
autoit3 ie.au3 函数之——_IEBodyReadHTML、_IEBodyWriteHTML
查看>>
autoit3 ie.au3 函数之——_IEBodyReadText
查看>>
autoit3 ie.au3 函数之——_IECreate
查看>>
autoit3 ie.au3 函数之——_IECreateEmbedded
查看>>
autoit3 ie.au3 函数之——_IEDocGetObj
查看>>
autoit3 ie.au3 函数之——_IEDocInsertHTML
查看>>
autoit3 ie.au3 函数之——_IEDocWriteHTML
查看>>
autoit3 ie.au3 函数之——_IEErrorHandlerDeRegister & _IEErrorHandlerRegister
查看>>
autoit3 ie.au3 函数之——_IEErrorNotify
查看>>
autoit3 ie.au3 函数之——_IEFormElementCheckBoxSelect & _IEFormGetObjByName
查看>>
autoit3 ie.au3 函数之——_IEFormElementGetCollection & _IEFormGetCollection
查看>>
watir测试报告(一)
查看>>
watir测试报告(二)
查看>>
watir——上传文件
查看>>
Python之读取TXT文件的三种方法
查看>>
Python之操作MySQL数据库
查看>>