转载请注明出处: http://qiudeqing.com/tools/2015/05/31/nginx.html

ubuntu 16 安装nginx

  1. 安装

     sudo apt-get update
     sudo apt-get install nginx
    
  2. 设置防火墙
  3. 检查状态systemctl status nginx

centos 6安装nginx

参考自how to install and configure nginx on centos 6

  1. 安装epel-release仓库

     yum install epel-release
    
  2. 安装nginx

     yum -y install nginx
    
  3. 启动nginx服务器

     service nginx start
    
  4. 配置系统启时启动nginx

     chkconfig nginx on
    

nginx基本使用

nginx配置文件为nginx.conf通常存放在/usr/local/nginx/conf,/etc/nginx或者/usr/local/etc/nginx目录下

启动,停止,从新加载配置

查看当前所有运行中得nginx进程

ps -ax | grep nginx

context上下文

Nginx配置文件{}组成上下文, 上下文可嵌套, 子上下文可继承父上下文设置.

每个上下文内只能包含它所允许的指令, 包含错误指令nginx会报错.

主上下文

最外层上下文叫全局上下文或主上下文:

# The main contex is here, outside any other context

...

context {
    ...
}

主上下文通常用来配置服务器的通用设置, 如worker数量, 保存主进程PID的文件路径.

事件上下文

事件上下文events包含在主上下文中, 用于全局设置如何处理连接, Nginx配置文件中只能存在一个事件上下文.

events {
    # events context
    ...
}

Nginx使用基于事件的连接处理模型, 所以事件上下文配置决定了worker进程如何处理连接. 这里配置主要用于选择连接处理技术, 或者修改这些方法的实现.

通常连接处理方法会自动选择平台最佳策略. 如Linux系统epoll通常是最佳选择.

其他配置用于配置一个worker可以处理的连接数, 收到挂起通知时是否挂起所有连接, worker是否轮流响应事件.

HTTP context

HTTP上下文包含处理HTTP和HTTPS连接所需的所有指令和上下文. HTTP上下文和events上下文都是主上下文内的兄弟上下文.

# main context

events {
    # events context
}

http {
    # http context
}

http上下文的指令定义了其内部包含的所有虚拟服务器的默认值. 常用指令有:

server上下文

server上下文声明在http上下文内, 可以有多个声明存在.

# main context

http: {
    # http context

    server {
        # first server context
    }
    server {
        # second server context
    }
}

用于区分server的参数:

location上下文

location上下文和server有很多共同点, 比如可以定义多个location上下文, 每个上下文处理特定的请求

location match_modified location_match {

}

location上下文需要定义在server上下文内, 允许嵌套这样就可以对特定请求进一步精确处理.

server {
    location /match/criteria {}
    location /other/criteria {
        location nested_match {}
        location other_nested {}
    }
}

server上下文用ip/端口号以及域名进行区分. location根据请求的URI对发送到所在server的请求进行划分处理.

上下文设置最佳实践

配置文件语法

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http 上下文设置

ngx_http_core_module

http {
    # basic settings
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_has_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # logging settings

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    #gzip setting
    gzip on;
    gzip_disable "misie6";
}

指令介绍:

虚拟主机配置

nginx.conf中http上下文通过include /etc/nginx/sites-enabled/*;引入配置文件, 这里的文件通常是/etc/nginx/sites-available/下配置文件的链接文件. 这样可以快速设置虚拟主机的启动配置.

下面是一个例子文件

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    server_name localhost;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

mac 默认nginx配置文件

#user  nobody;
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  1024;
}


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  0;
    keepalive_timeout  65;

    #gzip  on;
    include /usr/local/etc/nginx/myconf/*.conf;
    server {
        listen       8080;
        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;
        #}
    }


    # 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;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

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

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

参考资料