nginx笔记

阅读数:1601 发布时间:2016-05-09 23:34:21

作者:zzl005 标签: nginx 朱忠来005

nginx

高性能的HTTP和反向代理服务器

特点

  1. 配置非常简单(nginx.conf)
  2. 高并发,非阻塞(最高支持50000个并发)
  3. 负载均衡

安装

brew install nginx

查看版本

nginx -v:查看版本
nginx -V:查看版本和配置信息

运行

nginx -s reload    #重新加载配置
nginx -s reopen    #重启
nginx -s stop    #停止
nginx -s quit      #退出

配置文件

测试配置是否有错误

nginx -t #测试配置是否有错误

如果没有错误,则会提示:

nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok  
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

日志文件(log)

我本机的 nginx 目录为 :

但是nginx的日志文件目录为:

nginx日志文件下有三个文件:

127.0.0.1 - - [09/May/2016:22:24:13 +0800] "GET /src/js/browser.min.js HTTP/1.1" 200 1333640 "http://localhost:8091/src/index.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36"
2016/05/09 22:22:10 [error] 9539#0: *5 open() "/Users/abc/project/xyt/50x.html" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:8088"

方便通过 pid文件 kill –QUIT cat nginx.pid文件目录将nginx 的停止,不用再去查询 nginx 的pid。

典型nginx配置

#用户和组信息  
user  www www;

#工作进程
worker_processes  2;

#错误日志 
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  2048;
}

#http模块
http {
    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  65;

  # gzip压缩功能设置
    gzip on;
    gzip_min_length 1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 6;
    gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
    gzip_vary on;

  # http_proxy 设置
    client_max_body_size   10m;
    client_body_buffer_size   128k;
    proxy_connect_timeout   75;
    proxy_send_timeout   75;
    proxy_read_timeout   75;
    proxy_buffer_size   4k;
    proxy_buffers   4 32k;
    proxy_busy_buffers_size   64k;
    proxy_temp_file_write_size  64k;
    proxy_temp_path   /usr/local/nginx/proxy_temp 1 2;

  # 设定负载均衡后台服务器列表 
    upstream  backend  { 
              #ip_hash; 
              server   192.168.10.100:8080 max_fails=2 fail_timeout=30s ;  
              server   192.168.10.101:8080 max_fails=2 fail_timeout=30s ;  
    }

  # 很重要的虚拟主机配置
    server {
        listen       80;    #端口
        server_name  itoatest.example.com;
        root   /apps/oaapp;

        charset utf-8;
        access_log  logs/host.access.log  main;

        #对 / 所有做负载均衡+反向代理
        location / {
            root   /apps/oaapp;
            index  index.jsp index.html index.htm;

            proxy_pass        http://backend;  
            proxy_redirect off;
            # 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
            proxy_set_header  Host  $host;
            proxy_set_header  X-Real-IP  $remote_addr;  
            proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;

        }

        #静态文件,nginx自己处理,不去backend请求tomcat
        location  ~* /download/ {  
            root /apps/oa/fs;  

        }
        location ~ .*.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$   
        {   
            root /apps/oaapp;   
            expires      7d; 
        }
        location /nginx_status {
            stub_status on;
            access_log off;
            allow 192.168.10.0/24;
            deny all;
        }

        location ~ ^/(WEB-INF)/ {   
            deny all;   
        }
        #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;
        }
    }

  ## 其它虚拟主机,server 指令开始
}

本机配置:

# main全局配置
worker_processes  1;
error_log logs/erros.log;
#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

# 与提供http服务相关的一些配置参数
http {
    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;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    # 虚拟主机配置
    server {

        listen      8089;
        server_name localhost;

        #对 / 所有做负载均衡+反向代理
        location / {
                root /Users/abc/xiuyetang/;
                index index.html;

        }
   }

    server {
    listen           8091;
    server_name     localhost;
    location / {
       root /Users/abc/project/React/;
            index index.html;

    }

    include servers/*;
}

重点关注:http 服务中的 server(虚拟主机配置)

主要结构是:

http {

    ...

    server {
        # 监听的端口号
        listen                    8091;
        # 服务器名字,默认为 localhost 
        server_name            localhost;

        location / {
            # 代码位置
            # 当用root配置的时候,root后面指定的目录是上级目录
            # 并且该上级目录必须含有和location后指定的名称的同名目录,否则404
            # root末尾的"/"加不加无所谓
            root /Users/abc/project/React/;
                    # index字段声明了解析的后缀名的先后顺序
                    index index.html

        }

        # 404页面跳转到404.html,相对于上面的root目录
        error_page  404              /404.html;
        # 403页面跳转到403.html,相对于上面的root目录
        error_page  403              /403.html;
        # 50x页面跳转到50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    ...

}

参考:
前端工程师学习Nginx入门篇

相关文章推荐: