---
title: "Nginx的基本配置与优化"
author: "wosyingjun"
date: "2016-04-28T10:43:14Z"
tags: ["nginx"]
categories: ["开源软件"]
slug: "nginx"
draft: false
lastmod: "2016-04-28T10:42:16Z"
id: 2294512
url: "https://wosyingjun.iteye.com/blog/2294512"
---

## Nginx的基本配置与优化

Nginx的配置文件默认在Nginx程序安装目录的conf二级目录下面，主配置文件为nginx.conf，对nginx还不是很了解的可以看下我之前对Nginx介绍：

[http://wosyingjun.iteye.com/blog/2252941](/blog/2252941 "http://wosyingjun.iteye.com/blog/2252941")

#### Nginx 的配置组成：

```
....
events{
....
}
http{
....
    serer{
        ....
    }
    serer{
        ....
    }
....
}
```

#### Nginx 的基本配置：

```
user  root;  #使用的用户
worker_processes  2;  #指定工作衍生进程数（一般为总CPU核数）  
#工作模式及连接数上限
events {
    worker_connections  1024;  #单个后台worker process进程的最大并发链接数
}
#设定http服务器，利用它的反向代理功能提供负载均衡支持
http {
    include       mime.types;  #设定mime类型,类型由mime.type文件定义
    default_type  application/octet-stream;
    sendfile        on;  #普通应用设为on,磁盘IO重负载应用可设置为off
    keepalive_timeout  65;  #连接超时时间
    #gzip  on; #开启gzip可以加快页面的访问速度
    server {
        #侦听80端口
        listen       80; 
        #Nginx可以配置基于IP和域名的虚拟主机
        server_name  www.xxx.com;
        #设定本虚拟主机的访问日志
        access_log  logs/www.xxx.com.access.log  main;
        #定义服务器的默认网站根目录位置
        location / {
            root   html;
            index  index.html index.htm;
        }
        # 相关请求的跳转
        location /Key {
            proxy_pass  http://127.0.0.1:8880/Key/;
            proxy_set_header    Host        $host;
            proxy_set_header    X-Forwarded-For     $remote_addr;       
        }
        # 定义错误提示页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
     # SSL的配置
     server {
            listen       9457 ssl;
            server_name  localhost;
            # SSL证书和秘钥
            ssl_certificate      /usr/local/nginx/ssl/server.cer;
            ssl_certificate_key  /usr/local/nginx/ssl/server_key.pem;

            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;

            ssl_ciphers  HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers  on;
            # 相关请求的跳转
            location /Key {
                proxy_pass      http://127.0.0.1:8880/Key/;
                proxy_set_header        Host            $host;
                proxy_set_header        X-Forwarded-For         $remote_addr;
            }

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


}
```