Nginx系列-基础功能配置

  Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

Nginx基本配置

  打开nginx安装目录下的conf/nginx.conf配置文件,新增或修改一下内容:

worker_processes  1;

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  10000;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    #默认编码
    charset utf-8;
    #防止网络阻塞
    tcp_nopush on; 
    #防止网络阻塞
    tcp_nodelay on; 

    keepalive_timeout  60;

    #开启zip网页压缩
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css application/xml;

    #设定负载均衡的服务器列表
    upstream moto.bnuz163.com{
        server moto.bnuz163.com:8081 max_fails=1 fail_timeout=1s;
    }

    server {
        #侦听80端口
        listen             80;
        #域名可以有多个,用空格隔开
        server_name        moto.bnuz163.com;

        #对 “/” 启用反向代理
        location / {
            proxy_pass http://moto.bnuz163.com;
            proxy_connect_timeout 2s;

            proxy_redirect off; 
            proxy_set_header Host $host; #这一句至关重要
            proxy_set_header X-Real-IP $remote_addr; 
            #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #所有静态文件由nginx直接读取不经过tomcat
        location ~* \.(htm|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma|svg|js|css)$ {
            root   ./www/moto/;
            #缓存时间设置
            expires 1h;
        }

        #500、502、503、504页面设置
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   ./html;
        }

        #403、404页面重定向地址
        error_page  403 = http://moto.bnuz163.com:8081/error/403.html; 
        error_page  404 = http://moto.bnuz163.com:8081/error/404.html;

    }
}

  到此为止,基本配置已经完成了,其中http://moto.bnuz163.com我是指向了127.0.0.1本机地址的,因为在部署集群服务器时,许多配置要用到其他服务器ip,但是每次修改ip比较难记住,所以可以修改hosts文件,把moto.bnuz163.com指向一个ip。

修改hosts文件

  打开hosts文件,然后再最后一行添加上:

127.0.0.1   localhost

  完整配置如下:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       moto.bnuz163.com

120.28.xx.xx    server1.bnuz163.com

120.27.xx.xx    server2.bnuz163.com

115.28.xx.xx    server3.bnuz163.com

  Nginx基本配置已经完成,可以满足大部分人需求了。还有些不常用的配置,但是也很重要的,我会再后面会继续分享出来。

文章目录
  1. 1. Nginx基本配置
  2. 2. 修改hosts文件