常见的nginx的配置选项

Google 上有丰富的 Nginx 的教程和样本配置文件,但很多时候时候,配置这些是需要一些技巧。

Include 文件

不要在您的主 nginx.conf 文件中配置所有的东西,你需要分成几个较小的文件。您的同事会很感激你的。比如我的结构,我定义我的 upstream 的 pool 的为一个文件,和一个文件定义 location 处理服务器上其它的应用。

例子:
upstreams.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
upstream cluster1 {
  fair;
  server app01:7060;
  server app01:7061;
  server app02:7060;
  server app02:7061;
  }
upstream cluster2 {
  fair;
  server app01:7071;
  server app01:7072;
  server app02:7071;
  server app02:7072;
  }

locations.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
location / {
  root /var/www ;
  include cache-control.conf;
  index index.html index.htm;
  }
location /services/service1 {
  proxy_pass_header Server;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Scheme $scheme;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Pragma "no-cache" ;
proxy_pass http: //cluster1/ ;
  }
location /services/service2 {
  proxy_pass_header Server;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Scheme $scheme;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Pragma "no-cache" ;
proxy_pass http: //cluster2/service2 ;
  }

servers.conf

1
2
3
4
server {
  listen 80;
  include locations.conf;
  }

现在,你的 nginx.conf 看起来非常的干净和简单(仍然可以分开更多,来更包括文件,比如分离gzip的配置选项)

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
worker_processes 4;
  worker_rlimit_nofile 10240;
events {
  worker_connections 10240;
  use epoll;
  }
http {
  include upstreams.conf;
include mime.types;
  default_type application /octet-stream ;
log_format custom '$remote_addr - $remote_user [$time_local] '
  '"$request" $status $bytes_sent '
  '"$http_referer" "$http_user_agent" "$http_x_forwarded_for" $request_time' ;
access_log /usr/local/nginx/logs/access .log custom;
proxy_buffering off;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
gzip on;
  gzip_min_length 10240;
  gzip_proxied expired no-cache no-store private auth;
  gzip_types text /plain text /css text /xml text /javascript application /x-javascript application /xml application /xml +rss image /svg +xml application /x-font-ttf application /vnd .ms-fontobject;
  gzip_disable "MSIE [1-6]\." ;
# proxy cache config
  proxy_cache_path /mnt/nginx_cache levels=1:2
  keys_zone=one:10m
  inactive=7d max_size=10g;
  proxy_temp_path /var/tmp/nginx_temp ;
proxy_next_upstream error;
include servers.conf;
  }

这 nginx.conf 文件是使用了一些不太常见的配置选项,它值得指出其中一些重要的。

多个 worker 的配置(进程)

如果你的 Nginx 是多个 CPU 和多核,需要配置成多核的数量比较好:

1
worker_processes 4;

增加打开的文件句柄

如果 Nginx 服务很大的流量,增加最大可以打开的文件句柄还是很有用的,因为默认只有 1024 个.可以使用 ‘ulimit -n’ 看到当前系统中的设置.

1
worker_rlimit_nofile 10240;

定制的日志

可以看看 log_format 和 access_log 二个选项的设置. 通常我们有几个参数最常使用,象 “$http_x_forwarded_for” 可以见到 load balancer 的设备之前的 IP, 还有 “$request_time” 可以见到 Nginx 来处理这个主动所花的时间.

压缩

压缩对于文本非常非常的有用.

1
2
3
4
5
gzip on;
  gzip_min_length 10240;
  gzip_proxied expired no-cache no-store private auth;
  gzip_types text /plain text /css text /xml text /javascript application /x-javascript application /xml application /xml +rss image /svg +xml application /x-font-ttf application /vnd .ms-fontobject;
  gzip_disable "MSIE [1-6]\." ;

代理的选项

这些选项可以在每个 location 中设置.

1
2
3
4
5
6
7
proxy_pass_header Server;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Scheme $scheme;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  add_header Pragma "no-cache" ;

这个中加了一个定制的参数,就是 ‘no-cache’,这样就不会使用 cache 的内容了.

代理的 Cache

使用 Nginx 可以给一些文件来 cache 到本地来当 Cache 的服务器,需要设置 proxy_cache_path 和 proxy_temp_path 在你的 HTTP 的 directive 中.在 location 中配置.如果有你想 cache 的内容的话.

1
2
3
4
proxy_cache_path /mnt/nginx_cache levels=1:2
  keys_zone=one:10m
  inactive=7d max_size=10g;
  proxy_temp_path /var/tmp/nginx_temp ;

这可能还想增加一些其它的参数

1
2
3
4
5
6
proxy_cache one;
  proxy_cache_key mylocation.$request_uri;
  proxy_cache_valid 200 302 304 10m;
  proxy_cache_valid 301 1h;
  proxy_cache_valid any 1m;
  proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504 http_404;

HTTP caching options

有时你想使用其它的东西来做 Cache ,你可能需要指定怎么样 cache. 你可以给 cache 的信息的文件 include 到你的 root 的 location 中:

1
2
3
4
5
location / {
  root /var/www ;
  include cache-control.conf;
index index.html index.htm;
  }

你可以指定不同的头到于不同的文件

1
2
3
4
5
6
7
8
9
# default cache 1 day
  expires +1d;
if ($request_uri ~* "^/services/.*$" ) {
  expires +0d;
  add_header Pragma "no-cache" ;
  }
if ($request_uri ~* "^/(index.html)?$" ) {
  expires +1h;
  }

SSL

如果你要配置 ssl 的连接的话

1
2
3
4
5
6
7
8
server {
  server_name www.example.com;
  listen 443;
  ssl on;
  ssl_certificate /usr/local/nginx/ssl/cert .pem;
  ssl_certificate_key /usr/local/nginx/ssl/cert .key;
include locations.conf;
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Nginx 是一个高性能的开源 Web 服务器和反向代理服务器。它的配置文件是一个重要的部分,影响着服务器的行为和性能。下面是对 Nginx 配置文件的详细解释: 1. 配置文件位置: Nginx配置文件通常位于 /etc/nginx/nginx.conf。此外,可以通过 include 语句包含其他配置文件,使配置更加模块化。 2. 基本结构: Nginx配置文件由多个指令和块组成。指令是配置文件的最小单位,而块是由一对花括号括起来的指令集合。配置文件从上到下按顺序解析,遇到块会进一步解析块内的指令。 3. 主要指令: - worker_processes:指定 Nginx 创建的进程数。通常设置为 CPU 核心数的倍数。 - events:配置 Nginx 处理连接和请求的事件模型。 - http:定义 HTTP 服务器的全局配置。 - server:定义一个虚拟主机(或称为 server block),可以包含多个 location 块。 - location:根据请求的 URI 匹配规则,定义不同的请求处理方式。 4. 常用配置选项: - listen:指定监听的 IP 地址和端口。 - server_name:指定虚拟主机的域名。 - root:指定网站根目录。 - index:指定默认的索引文件。 - location:根据匹配规则指定不同的处理方式,如代理、重定向、静态文件处理等。 5. 变量和指令: Nginx 支持一些内置变量和指令,用于实现动态的配置和请求处理。常见的变量包括 $uri、$args 和 $http_user_agent,常见的指令包括 if、rewrite 和 return。 6. 配置文件的重载: 在修改配置文件后,可以通过发送信号给 Nginx 进程来重新加载配置,而无需停止服务器。常用的信号有 HUP(重新加载配置)和 TERM(停止服务器)。 这些是 Nginx 配置文件的一些基本概念和常用指令。具体的配置方式和使用方法可以根据实际需求进行深入学习和探索。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值