Web服务器Nginx常见的配置选项整理

Google上有丰富的 Nginx 的教程和样本配置文件,但很多时候时候,配置这些是一些技巧,一直对大家很有帮助。

Include 文件

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

例子:

upstreams.conf

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

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

server {listen 80;include locations.conf;}

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

nginx.conf

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 configproxy_cache_path /mnt/nginx_cache levels=1:2keys_zone=one:10minactive=7d max_size=10g;proxy_temp_path /var/tmp/nginx_temp;proxy_next_upstream error;include servers.conf;}

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

多个 worker 的配置(进程)

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

worker_processes 4;

增加打开的文件句柄

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

worker_rlimit_nofile 10240;

定制的日志

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

压缩

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

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 中设置

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 的内容的话。

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

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

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 中

location / {root /var/www;include cache-control.conf;index index.html index.htm;}

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

# default cache 1 dayexpires +1d;if ($request_uri ~* "^/services/.*$") {expires +0d;add_header Pragma "no-cache";}if ($request_uri ~* "^/(index.html)?$") {expires +1h;}

SSL

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

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)

相关推荐

  • CentOS-6.3系统中安装配置Web服务器Nginx

    Nginx 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。nginx的并发能力确实在同类型的网页伺服器中表现较好.Nginx在一些Linux发行版和B ...

  • Web服务器的安装与配置

    Web服务器主要功能是提供网上信息浏览服务. 一.WEB服务器的介绍: 01 1.WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务. 2.最主流的三个We ...

  • 如何在Ubuntu上搭建一台安全的Apache Web服务器?

    本教程假设你已有一台在运行的Ubuntu服务器,网络方面已设置好,而且可以通过SSH进行访问. Apache2是许多安装的Linux发行版使用的默认Web服务器.它不是对所有环境来说唯一可用的Web服 ...

  • win10怎么搭建web服务器 win10搭建web服务器的方法

    win10搭建web服务器的方法 首先打开win10系统,找到屏幕下方的文件资源管理器 打开后进入了磁盘管理界面,打开左边的选项,找到这台电脑这个选项,鼠标右键打开并找到属性 打开后出现系统版本与其他 ...

  • linux搭建nginx WEB服务器的教程

    下文来为各位介绍一篇在linux搭建nginx WEB服务器的教程,如果各位不想使用apache环境了,想用nginx环境就可以和小编一起来看看。 1、下载nginx 命令:wget http://n ...

  • window7系统下配置IIS搭建web服务器的步骤

    window7系统下配置IIS搭建web服务器的步骤 IIS全名Internet Information Services(IIS,互联网信息服务),在之前的在Windows 2000、Windows ...

  • 解决安装Domino for Linux时配置的Web服务器无法启动的问题

    环境: 产品: Lotus Domino for Linux 平台: Linux 版本: Lotus Domino for Linux 5.0.x 问题 : 安装Domino for Linux ...

  • Ubuntu 11.0下配置Web服务器详细教程[ 图文]

    近日,有消息爆出Linux桌面已经趋于“死亡”,众多Linux爱好者感到失望,但令人可喜的是Linux在服务器领域表现却尤为喜人,据IDC数据显示,部署了Linux系统的服务器占到20%的市场份额。其 ...

  • nginx服务器的安装和配置的方法介绍

    nginx服务器的安装和配置 1.安装 1)在windows环境下的安装 下载windows版本的nginx后,解压,然后进入到目录中,运行:start nginx 其他命令:nginx -s [ s ...