---
title: "Nginx学习（四）配置文件"
author: "jja1982"
date: "2012-07-11T15:36:21Z"
tags: []
categories: ["企业架构"]
slug: "nginx"
draft: false
lastmod: "2012-07-11T15:44:06Z"
id: 1585092
url: "https://jja1982.iteye.com/blog/1585092"
---

#使用的用户组
user www www;
#指定工作延伸进程数，一般等于CPU的总核数
worker_processes auto;
#指定错误日志存放的路径，错误日志记录级别可选项为[debug| info| notice| warn| error | crit]
error_log logs/error.log crit;
#指定pid存放的路径
pid /usr/local/nginx/logs/nginx.pid
#指定文件描述符数量
worker_rlimit_nofile 51200；

events {
    #使用的网络I/O模型，Linux系统推荐采用epoll模型，FreeBSD系统推荐采用kqueue模型
    use epoll;
    #允许的连接数
    worker_connections 51200;
}

http {
    include mime.types;
    default_type application/octet-stream;
    #设置静态页面目录
    root html;
    #是指长连接，0为短连接
    keepalive_timeout 0;
    #设置日志格式
    log_format access '$remote_addr - $remote_user[$time_local] "$request" ' '$status $body_byte_sent"$http_refer"' '"$http_user_agent"$http_x_forwarded_for';
    #是指access log
    access_log logs/access.log access;
    #开启gzip压缩
    gzip on;
    #由http反向代理Tomcat HTTP服务器
    upstream tomcat_server{
        server 127.0.0.1:8080;
    }
    server {
        listen 80;
       	server_name www.yourdomain.com yourdomain.com;
        #默认首页地址，顺序从左到右
        index index.html index.htm index.php;
        location ~ \.(jsp|jspx|do)?$ {
            proxy_set_header HOST $host
            proxy_set_header X-Forwarded-FOR $remote_addr;
            proxy_pass http://tomcat_server;
	}
    }
	
    #负载均衡
    upstream php_server_pool {
         server 192.168.1.10:80 weight=4 max_fails=2 fail_timeout=30s;
         server 192.168.1.11:80 weight=4 max_fails=2 fail_timeout=30s;
         server 192.168.1.12:80 weight=2 max_fails=2 fail_timeout=30s;
    }
    server {
         listen 80;
         server_name php.yourdomain.com;
         location / {
             proxy_pass http://php_server_pool;
         }
    }
    access_log logs/access.log combined;
}