nginx配置在nginx根目录的conf中,名字为nginx.conf。。默认内容大概如下
1 #user nobody; 2 worker_processes 1; # 设置工作的进程数目,可以根据cpu核数来设置 3 # 错误日志,打开后监听logs下error_log文件可以查看错误日志 4 #error_log logs/error.log; 5 #error_log logs/error.log notice; 6 #error_log logs/error.log info; 7 #error_log "pipe:rollback logs/error_log interval=1d baknum=7 maxsize=2G"; 8 9 #pid logs/nginx.pid; 10 11 12 events { 13 worker_connections 1024; 14 } 15 16 # load modules compiled as Dynamic Shared Object (DSO) 17 # 18 #dso { 19 # load ngx_http_fastcgi_module.so; 20 # load ngx_http_rewrite_module.so; 21 #} 22 23 http { 24 include mime.types; 25 default_type application/octet-stream; 26 # 日志的存储,显示格式 27 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 28 # '$status $body_bytes_sent "$http_referer" ' 29 # '"$http_user_agent" "$http_x_forwarded_for"'; 30 31 #access_log logs/access.log main; 32 #access_log "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G" main; 33 34 sendfile on; 35 #tcp_nopush on; 36 37 #keepalive_timeout 0; 38 keepalive_timeout 65; 39 40 #gzip on; # 文件压缩传输 41 # 每一个server都是一个虚拟主机,可以运行多个服务 42 # 创建一个负载均衡池,名字随意,server可以写多个,对应IP地址。。如:myproject 43 44 upstream myproject { 45 #负载均衡的方式,默认是轮询 46 #还有其他的负载均衡的规则 47 server 192.168.168.131; 48 server 192.168.168.132; 49 } 50 #负载均衡的规则 51 #调度算法 概述 52 #轮询 按时间顺序逐一分配到不同的后端服务器(默认) 53 #weight 加权轮询,weight值越大,分配到的访问几率越高,最常用的方式, 54 #ip_hash 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器 55 #url_hash 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器 56 #least_conn 最少链接数,那个机器链接数少就分发 57 58 59 server { 60 # 监听80端口 61 listen 80; 62 server_name localhost; # 主机名,可以是IP,网址,或者localhost 63 64 #charset koi8-r; 65 # 66 #access_log logs/host.access.log main; 67 #access_log "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G" main; 68 69 location / { 70 root html; # root为虚拟主机根目录,相对路径,既所有东西都从nginx的目录中的html文件夹中查找, 71 index index.html index.htm; # 主页文件名 72 } 73 74 location / { 75 # 通过上面已经设置的负载均衡池,就可以直接代理转发到负载均衡池中。 76 proxy_pass http://myproject; 77 78 #uwsgi_pass 127.0.0.1:8000; # 本行和下行是通过uwsgi进行请求转发 79 #include uwsgi_params; # 包含uwsgi_params文件 80 } 81 82 # location后面通过正则匹配url地址,如果是静态文件地址,(自己将静态文件地址归类)直接nginx进行返回,做动静分离时使用 83 location /static { 84 alias /opt/static; # url匹配成功去别名的路径中查找并返回 85 } 86 87 #error_page 404 /404.html; # 404错误文件名 88 89 # redirect server error pages to the static page /50x.html 90 # 91 error_page 500 502 503 504 /50x.html; 92 location = /50x.html { 93 root html; 94 } 95 96 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 97 # 98 #location ~ .php$ { 99 # proxy_pass http://127.0.0.1; # 代理转发,将请求转发到某个ip或网址上 100 #} 101 102 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 103 # 104 #location ~ .php$ { 105 # root html; 106 # fastcgi_pass 127.0.0.1:9000; 107 # fastcgi_index index.php; 108 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 109 # include fastcgi_params; 110 #} 111 112 # deny access to .htaccess files, if Apache's document root 113 # concurs with nginx's one 114 # 115 #location ~ /.ht { 116 # deny all; # 拒绝访问的路径,all表示所有 117 #} 118 } 119 120 121 # another virtual host using mix of IP-, name-, and port-based configuration 122 # 123 #server { 124 # listen 8000; 125 # listen somename:8080; 126 # server_name somename alias another.alias; 127 128 # location / { 129 # root html; 130 # index index.html index.htm; 131 # } 132 #} 133 134 135 # HTTPS server 136 # 137 #server { 138 # listen 443 ssl; # 监听https的443端口 139 # server_name localhost; 140 141 # ssl_certificate cert.pem; 142 # ssl_certificate_key cert.key; 143 144 # ssl_session_cache shared:SSL:1m; 145 # ssl_session_timeout 5m; 146 147 # ssl_ciphers HIGH:!aNULL:!MD5; 148 # ssl_prefer_server_ciphers on; 149 150 # location / { 151 # root html; 152 # index index.html index.htm; 153 # } 154 #} 155 156 }
上面注释已经写得很清楚了,,每个配置及功能作用。。
在用nginx做负载均衡时,就设置upstream 标签中的负载主机设置。
在用nginx做动静分离时,就设置location 的匹配条件,并设置alias /opt/static;到本地文件夹查找静态文件。。
在用nginx做反向代理时,就直接在location 中设置proxy_pass 参数,到要跳转的IP或网址。
如果要用nginx实现多虚拟主机,就直接配置多个server标签即可。。