`

Nginx常用配置

 
阅读更多

数据为王

Data is the new oil.
 
首页 > Nginx > Nginx 常用配置

Nginx 常用配置

不多说废话,直接上nginx.conf简洁版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#使用哪个用户启动nginx
user  frankwong;
 
#nginx 工作进程数,一般设置成CPU核数
worker_processes  4;
 
# [ debug | info | notice | warn | error | crit ]   错误日志的位置
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#nginx进程号保存文件
#pid        logs/nginx.pid;
 
events {
    #use [ kqueue | rtsig | epoll | /dev/poll | select | poll ] 使用epoll(linux2.6的高性能方式)
    use epoll;
 
    #每个worker最大连接数,受限于进程最大打开文件数目,参考ulimit -n
    worker_connections  1024;
}
 
http {
     #文件扩展名与文件类型映射表
    include       mime.types;
 
    #默认文件类型 bin exe dll
    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;
 
    #上传文件大小限制
    client_max_body_size 20m
    #设定请求缓冲  
    client_header_buffer_size 1k;  
    large_client_header_buffers 4 4k;
 
    server {
        #监听端口号
        listen       80;
        #配置基于名称的虚拟主机,通过它可以进行多域名转发
        server_name  localhost;
 
        #默认编码
        charset utf-8;
 
        #设定本虚拟主机的访问日志
        #access_log  logs/host.access.log  main;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
        #错误页面
        error_page  404              /404.html;
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        
    }
}

1.开启gzip压缩

找到#gzip on这行,修改成如下内容即可

1
2
3
4
5
6
7
8
9
gzip            on;
gzip_min_length 1k;//只压缩大于1K的文件
gzip_buffers 4 16k;
#gzip_http_version 1.0;//默认1.1
gzip_comp_level 2;//压缩级别,1-10,数字越大压缩的越好,时间也越长
gzip_min_length     1000;
gzip_types      text/plain text/css application/x-javascript;
gzip_vary off;//Squid等缓存服务有关,on的话会在Header里增加"Vary: Accept-Encoding"
gzip_disable "MSIE [1-6]."; //取消对IE6的支持

2.设置Nginx 监控

1
2
3
4
5
6
location ~ ^/NginxStatus {
           stub_status on;//开启状态查看
           access_log off;//关闭日志
           allow   127.0.0.1;//设置仅能本机查看
           deny    all;
}

生效之后输入http://127.0.0.1/NginxStatus 即可看到相关状态信息

1
2
3
4
Active connections: 328  
server accepts handled requests  
9309 8982 28890  
Reading: 1 Writing: 3 Waiting: 324

3.设置静态资源的缓存

1
2
3
4
5
location ~ .(htm|html|gif|jpg|jpeg|png|ico|rar|css|js|zip|txt|flv|swf|doc|ppt|xls|pdf)$ {
           root  /opt/webapp/cache;//静态资源路径
           access_log off;
           expires 24h;//cache有效时间 这边设置成1
}

4.设置图片防盗链

1
2
3
4
5
6
7
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)${
       expires      30d;
       valid_referers none blocked dropbag.sinaapp.com*;
       if ($invalid_referer) {
       rewrite ^/ http://gitsea.com/404.html;#return 404;
       }
}

5.平滑变更nginx配置
输入

1
/usr/local/nginx/sbin/nginx -t

如果结果显示

1
2
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully

表示没有问题,则可以进行平滑重启,输入如下指令
nginx -s reload
其他常用指令如
nginx -s stop 停止nginx服务

6.日志切割
nginx的日志文件没有rotate功能。如果你不处理,日志文件将变得越来越大,所以需要写一个nginx日志切割脚本来自动切割日志文件。

第一步就是重命名日志文件,不用担心重命名后nginx找不到日志文件而丢失日志。在你未重新打开原名字的日志文件前,nginx还是会向你重命名的文件写日志,linux是靠文件描述符而不是文件名定位文件。
第二步向nginx主进程发送USR1信号。
nginx主进程接到信号后会从配置文件中读取日志文件名称,重新打开日志文件(以配置文件中的日志名称命名),并以工作进程的用户作为日志文件的所有者。
脚本文件nginx_log.sh如下

1
2
3
4
5
6
7
8
9
10
11
12
#nginx日志切割脚本
#!/bin/bash
#设置日志文件存放目录
logs_path="/usr/local/nginx/logs/"
#设置pid文件
pid_path="/usr/local/nginx/nginx.pid"
 
#重命名日志文件
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
 
#向nginx主进程发信号重新打开日志
kill -USR1 `cat ${pid_path}`

crontab 设置作业
0 0 * * * bash /usr/local/nginx/nginx_log.sh
这样就每天的0点0分把nginx日志重命名为日期格式,并重新生成今天的新日志文件。

7.反向代理和负载均衡
负责均衡需功能需要在编译nginx的时候把ngx_http_upstream_module模块编译进去
假设有3台tomcat服务器ip分别为192.168.1.101,192.168.1.102,192.168.0.103,我们需要用nginx对这3台tomcat服务器做负载均衡,使得在高访问量的情况下能够对req做分发。
配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
upstream tomcatserver{  
#weigth参数表示权值,权值越高被分配到的几率越大   
server 192.168.1.101:8080  weight=5;  
server 192.168.1.102:8080  weight=1;  
server 192.168.1.103:8080  weight=6;  
}
#对 "/" 启用负载均衡  
location / {  
proxy_pass http://tomcatserver;  
proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;//通过X-Forwarded-For获取用户真实IP
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;//允许客户端请求的最大单文件字节数
client_body_buffer_size 128k;//缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout   300;//nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout      300;//后端服务器数据回传时间(代理发送超时)
proxy_read_timeout      300;//连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size       4k;//设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;//高负荷下缓冲大小
proxy_temp_file_write_size 64k;//设定缓存文件夹大小,大于这个值,将从upstream服务器传
}

常用命令

-c </path/to/config> 为 Nginx 指定一个配置文件,来代替缺省的。

-t 不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。

-v 显示 nginx 的版本。

-V 显示 nginx 的版本,编译器版本和配置参数。

问题
关于nginx并发数的计算,参加Nginx Wiki

1
2
3
4
5
6
7
8
worker_connections
Syntax: worker_connections number
Default:
The worker_connections and worker_proceses from the main section allows you to calculate maxclients value:
max_clients = worker_processes * worker_connections
In a reverse proxy situation, max_clients becomes
max_clients = worker_processes * worker_connections/4
Since a browser opens 2 connections by default to a server ,and nginx uses the fds (file descriptors) from the same pool to connect to the upstream backend .

翻译
做http服务:浏览器只有1个连接 所以max_clients = worker_processes * worker_connections
做反向代理:从浏览器到nginx,然后从nginx到后端, 从后端到nginx,再由nginx到浏览器。max_clients = worker_processes * worker_connections/4

 
 
 
  1. 本文目前尚无任何评论.
 
 昵称 (必填)
 电子邮箱 (我们会为您保密) (必填)
 网址
验证图片
刷新验证码
 验证码 *
 
回到顶部WordPress
版权所有 © 2013 数据为王
主题由 NeoEase 提供, 通过 XHTML 1.1  CSS 3 验证.
分享到:
评论

相关推荐

    nginx.conf nginx常用配置

    nginx.conf nginx常用配置

    Nginx常用配置、负载均衡及优化.ppt

    Nginx常用配置、负载均衡及优化

    Nginx常用配置、负载均衡及优化

    Nginx常用配置、负载均衡及优化

    nginx常用配置方法

    web建站常用nginx配置参数详解,可以做到基本的通用配置。(虚拟主机,反向代理,域名跳转)

    nginx常用配置文件

    nginx的一些常用配置文件

    nginx常用bat批处理命令

    nginx常用bat批处理命令,放在nginx同级目录下使用,可快速的重启、停止、关闭nginx。 quit.bat(退出Nginx) reload.bat(重启Nginx) stop.bat(停止Nginx)

    nginx-common-configuration:Nginx常用配置

    Nginx常用配置 Nginx配置。 不是最强大,最有生产力或最好的。 只是有用的配置,我想在开箱即用的默认Nginx包中看到 :grinning_squinting_face: 奖励:nginx的fail2ban,filebeat和docker-compose配置:) 警告:我住...

    Nginx的常用配置文件

    Nginx的常用配置文件,适合负载均衡设置等

    nginx1.19.1以及常用配置文档.zip

    nginx最新版本1.19.1以及常用的一些配置文档,本文档自己总结,nginx从官网先下载的。大家若有疑问,可以看我的博客。

    Nginx配置学习资料.pdf

    Nginx配置学习资料: Nginx常用功能 Nginx配置文件结构 Nginx代理服务的配置说明 Nginx负载均衡详解

    nginx正向配置和反向配置

    nginx正向配置和反向配置 适用于中高级同学 企业内部映射、隧道常用

    实战Nginx高性能Web服务器

    4、高性能Web服务器Nginx的配置与部署研究(4)Nginx常用命令 内容:Nginx部署中常用的命令,包括启动、测试、停止、发送信号等。 5、高性能Web服务器Nginx的配置与部署研究(5)Nginx配置符号 内容:这篇简短的...

    nginx正向代理https和非80端口配置文档.pdf

    nginx代理功能十分强大,经常用来做反向代理,但有的实际工作环境需要正向代理,经安装发现网上常用的方法有两点问题,1.并不支持https代理,2需要访问的网址一但加上非80端口就会403报错。 后经反复测试,已找到...

    Nginx配置命令

    Nginx常用的配置命令

    nginx日常维护常用命令

    一、简明nginx常用命令 1. 启动 Nginx 代码如下:poechant@ubuntu:sudo ./sbin/nginx2. 停止 Nginx 代码如下:poechant@ubuntu:sudo ./sbin/nginx -s stoppoechant@ubuntu:sudo ./sbin/nginx -s quit-s都是采用向 ...

    Nginx高性能WEB服务器视频.rar

    3 Nginx常用命令管理及升级.rar 4 Nginx配置文件精讲一.rar 5 Nginx配置文件精讲二及多站点配置。rar 6企业实战Nginx+Tomcati动静分离架构.rar 7企业实战Nginx+PHP(FastCGI)高性能服务器.rar 8 Nginx与Tomcat-PHP...

    Nginx安装配置、Resin安装配置说明文档

    b)常用的 Nginx 参数 - 3 - c)静态文件处理 - 4 - d)动态页面请求处理 - 4 - e)下面为nginx.conf配置实例: - 5 - f)Nginx 启动,停止等命令 - 8 - (2) Resin安装配置 - 9 - 1) Resin安装 - 9 - 2) Resin配置 - 9 - ...

    windows下配置nginx反向代理tomcat

    windows下配置nginx反向代理tomcat,从下载地址开始讲起,第二步讲常用命令 第三步讲了一个实例,第四步是配置说明

    Nginx课件和笔记.rar

    06.nginx常用的命令 07.nginx的配置文件 08.nginx配置实例(反向代理准备工作) 09.nginx配置实例(反向代理实例一) 10.nginx配置实例(反向代理实例二) 11.nginx配置实例(负载均衡) 12.nginx配置实例...

    实战nginx-张宴

    3.6 Nginx的浏览器本地缓存设置 第4章 Nginx与PHP( FastCGI) 的安装、 配置与优化 4.1 获取相关开源程序 4.2 安装PHP 5.2.10( FastCGI模式) 4.3 安装Nginx 0.8.15 4.4 配置开机自动启动Nginx+PHP 4.5 优化Linux...

Global site tag (gtag.js) - Google Analytics