nginx入门之基本配置

nginx入门之基本配置

一、基础配置

1.1 nginx.conf结构

... #全局块
event {
    ...
}
http {               #http块
    ...                 #http全局块
    server {        #server块
        ...     #server 全局
        location [PATTERN] { #location 块
            ...            
        }
         location [PATTERN] {
            ... 
        }
    }
    server {

        ... 
    }
    ...
}
  1. 全局块:配置用户、worker数,pid存放路径,日志路径,配置文件引入等
  2. events:主要配置服务器与用户的网络连接(事件驱动模型,最大网络连接数等)
  3. http:代理、缓存、日志定义、超时时间、MIME-Type、是否sendfile传输文件等
  4. server:主要针对虚拟主机的配置

1.2 配置运行用户

user root root;

如上,设置nginx只有root权限才能启动,如果要设置为所有用户都能启动,可以:

  1. 注释user
  2. 设置用户和用户组为nobody

1.3 配置worker process数

worker_process number | auto;
  • number :指定worker数,推荐和系统内核数一样
  • auto: nginx进程自定检测

1.4 示例

user nobody nobody;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 65535;
error_log  /usr/local/nginx/logs/nginx_error.log crit;
pid        /usr/local/nginx/logs/nginx.pid;
events 
{
  use epoll;
  worker_connections 65535;
}
http 
{
  include       mime.types;
  default_type  application/octet-stream;
  charset utf-8;  
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m; 
  sendfile on;
  tcp_nopush     on;
  keepalive_timeout 10;
  server_tokens    off;
  tcp_nodelay on;
  add_header Server-ID $hostname;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 256k;
  fastcgi_buffers 2 256k;
  fastcgi_busy_buffers_size 256k;
  fastcgi_temp_file_write_size 256k;
  fastcgi_intercept_errors on;
  #add for compress
  gzip on;
  gzip_min_length   1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 6;
  gzip_types  text/plain application/x-javascript text/css  application/xml;
  gzip_vary on;
  postpone_output 1460;
  gzip_proxied        any;
  gzip_disable        "MSIE [1-6]\.";
  client_body_buffer_size  512k;
  proxy_connect_timeout    600;
  proxy_read_timeout       600;
  proxy_send_timeout       600;
  proxy_buffer_size        8k;
  proxy_buffers            4 32k;
  proxy_busy_buffers_size 64k;
  proxy_temp_file_write_size 64k;
  proxy_temp_path   /tmp/proxy_temp_dir;
  proxy_cache_path  /tmp/proxy_cache_dir  levels=1:2   keys_zone=cache_one:9192m inactive=1d max_size=30g;
  proxy_set_header        Host            $host;
  proxy_set_header        Proxy-Client-IP       $remote_addr;
  proxy_set_header        X-Forwarded-For $http_x_forwarded_for;
  server {
        listen       80;
        server_name  localhost;
        location / {
                 return 200 "<h1>not found</h1>";
                 expires      30d;
        }
  }
  log_format nginxjson '{"@timestamp":"$time_iso8601",'
               '"@fields": { '
               '"@version":"1",'
               '"host":"$server_addr",'
               '"remote_addr":"$remote_addr",'
               '"http_x_forwarded_for":"$http_x_forwarded_for",'
               '"request_method":"$request_method",'
               '"domain":"$host",'
               '"url.raw":"$uri",'
               '"url":"$scheme://$http_host$request_uri",'
               '"status":"$status",'
               '"server_protocol":"$server_protocol",'
               '"size":$body_bytes_sent,'
               '"responsetime":$request_time,'
               '"http_referer":"$http_referer",'
               '"upstr_addr": "$upstream_addr",'
               '"upstr_status": "$upstream_status",'
               '"ups_resp_time": "$upstream_response_time",'
               '"accept":"$http_accept",'
               '"agent": "$http_user_agent"}';
  access_log   /usr/local/nginx/logs/access.log nginxjson;
  include conf.d/*.conf;

conf.d目录下配置server

二、虚拟主机配置

一个nginx可以配置多个虚拟主机,也就是常说的多个域名配置。也就是说,一个nginx可以配置多个server,多个server都可以监听80端口,只需要域名不同即可。

upstream up1 {
  server 192.268.0.1:8080;
  server 192.268.0.2:8080;
}
upstream up2 {
  server 192.268.0.3:8080;
  server 192.268.0.4:8080;
}
server{
  listen 80;
  server_name example1.com;   #可以是域名,可以是ip,多个域名用`空格`隔开
  access_log logs/access1_log;#每一个server都可以配置自己的日志文件,如果不配做,走http模块默认。
  error_log logs/error1_log;

  location / {
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For "$http_x_forward_for,$remote_addr";
    proxy_pass http://up1;
  }
}
server{
  listen 80;
  server_name example2.com;
  access_log logs/access2_log;
  error_log logs/error2_log;

  location / {
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For "$http_x_forward_for,$remote_addr";
    proxy_pass http://up2;
  }
}

示例:配置一个hexo静态博客网站。

server {
  listen 80;
  server_name thoreauz.com;
  root /var/www/hexo;#博客文件位置
  access_log logs/hexo_access_log;
  error_log logs/hexo_error_log;
  location ~* ^.+\.(ico|git|jpg|png)$ {
    root /var/www/hexo;
    access_log off;
    expires 1d;#图片缓存1小时
  }
  location ~* ^.+\.(css|html|js|xml|swf|wav)$ {
    root /var/www/hexo;
    access_log off;
    expires 10m;#js等文件缓存时间10分支
  }
  location / {
    root /var/www/hexo;
    if (-f $reques_filename){
      rewrite ^/(.*)$ /$1 break;
    }
  }
}
CONTENTS