---
title: "ubuntu nginx 安装以及配置文件详解"
author: "linmomo02"
date: "2012-04-19T18:33:20Z"
tags: []
categories: ["企业架构"]
slug: "ubuntu-nginx"
draft: false
lastmod: "2012-04-19T18:33:20Z"
id: 1490657
url: "https://linmomo02.iteye.com/blog/1490657"
---

1.到nginx官网下载源码包。最好下载稳定版本，nginx官网http://www.nginx.org/

2.安装nginx依赖包运行命令：

sudo apt-get install libssl-dev  
sudo apt-get install libpcre3 libpcre3-dev

3.解压下载的nginx源码包。检查编译环境 ./configure --prefix=/usr/local/nginx

4.编译安装 make && make install

5.到nginx安装目录执行启动命令：/usr/local/nginx/sbin/nginx

关闭命令：/usr/local/sbin/nginx -s stop (停止)  
重启命令：/usr/local/sbin/nginx -s reload (重启)

启动之后浏览器中http://localhost/查看是否成功。

 

6.nginx配置文件所在目录/usr/local/nginx/conf/nginx.conf

配置文件详解如下：

#运行用户  
#user  nobody;  
#启动进程  
worker\_processes  1;

#全局错误日志及PID文件  
#error\_log  logs/error.log;  
#error\_log  logs/error.log  notice;  
#error\_log  logs/error.log  info;

#pid        logs/nginx.pid;

#工作模式及连接数上限  
events {  
    use epoll;  
       worker\_connections  1024;  
}

#设定http服务器，利用它的反向代理功能提供负载均衡支持  
http {  
    #设定mime类型  
    include       mime.types;  
    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;  
        #客户端和nginx之间空闲链接超时时间  
        keepalive\_timeout  120;

    #设定负载均衡的服务器列表  
    upstream mysvr {  
        #weigth参数表示权值，权值越高被分配到的几率越大  
        #本机上的Squid开启3128端口  
        server 127.0.0.1:8080 weight=1;  
        #server 119.254.82.237:8080  weight=1;  
    }  
    #是否开启gzip模块  
    #gzip  on;  
    #gzip\_min\_length  1100;  
    #gzip\_buffers    4 8k;  
    #gzip\_types      text/plain;  
    #设定虚拟主机  
    server {  
        listen       80;  
        server\_name  localhost;  
        #charset koi8-r;  
        #设定本虚拟主机的访问日志  
        #access\_log  logs/host.access.log  main;  
        #location / {  
        #    root   html;  
        #    index  index.html index.htm;  
               # }  
        #error\_page  404              /404.html;  
        # redirect server error pages to the static page /50x.html  
        #  
        error\_page   500 502 503 504  /50x.html;  
        location = /50x.html {  
            root   html;  
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
        #  
        #location ~ \\.php$ {  
        #    proxy\_pass   http://127.0.0.1;  
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
        #  
        #location ~ \\.php$ {  
        #    root           html;  
        #    fastcgi\_pass   127.0.0.1:9000;  
        #    fastcgi\_index  index.php;  
        #    fastcgi\_param  SCRIPT\_FILENAME  /scripts$fastcgi\_script\_name;  
        #    include        fastcgi\_params;  
        #}

        # deny access to .htaccess files, if Apache's document root  
        # concurs with nginx's one  
        #  
        #location ~ /\\.ht {  
        #    deny  all;  
        #}

        #对 "/" 启用负载均衡  
        location / {  
            proxy\_pass      http://mysvr;  
            proxy\_redirect          off;  
            proxy\_set\_header        Host $host;  
            proxy\_set\_header        X-Real-IP $remote\_addr;  
            proxy\_set\_header        X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
            #客户端body中最大数据量  
            client\_max\_body\_size    10m;  
            client\_body\_buffer\_size 128k;  
            # 链接到主机超时时间  
            proxy\_connect\_timeout  90;  
            # 向主机发送数据超时时间  
            proxy\_send\_timeout      90;  
            # 从主机读取数据超时时间  
            proxy\_read\_timeout      90;  
            proxy\_buffer\_size      4k;  
            proxy\_buffers          4 32k;  
            proxy\_busy\_buffers\_size 64k;  
            proxy\_temp\_file\_write\_size 64k;  
        }  
    }  
    # another virtual host using mix of IP-, name-, and port-based configuration  
    #server {  
    #    listen       8000;  
    #    listen       somename:8080;  
    #    server\_name  somename  alias  another.alias;

    #    location / {  
    #        root   html;  
    #        index  index.html index.htm;  
    #    }  
    #}  
}