1.Nginx编译配置命令详解
prefix=path — 定义服务器文件的完整路径,该路径同时也是configure命令设置的 相对路径(除类库源文件外)以及nginx.conf文件定义的相对路径的基准路径。其默认 值是/usr/local/nginx。
–sbin-path=path — 设置nginx可执行文件的完整路径,该路径仅在安装期间使用, 默认路径为prefix/sbin/nginx。
–conf-path=path — 设置配置文件nginx.conf的完整路径。如有必要,总是可以 在nginx启动时通过命令行参数-cfile指定一个不同的配置文件路径。 默认路径为prefix/conf/nginx.conf。
–pid-path=path — 设置nginx.pid文件的完整路径,该文件存储了主进程的进程ID。安装完成后, 该路径总是可以在nginx.conf文件中用 pid指令来修改。 默认路径为prefix/logs/nginx.pid。
–error-log-path=path — 设置记录主要错误、警告以及调试信息日志的完整路径。安装完成后, 该路径总是可以在nginx.conf文件中用 error_log 指令来修改。 默认路径为prefix/logs/error.log。
–http-log-path=path — 设置记录HTTP服务器主要请求日志的完整路径。安装完成后, 该路径总是可以在nginx.conf文件用 access_log 指令来修改。 默认路径为prefix/logs/access.log
–user=name — 设置工作进程以某非特权用户的身份运行。安装完成后,该用户名总是可以在 nginx.conf文件用user指令来修改。 其默认值为nobody。
–group=name — 设置工作进程以某非特权用户的身份运行。安装完成后,该用户名总是可以在 nginx.conf用user指令来修改。 其默认名称与未授权用户名称相同。
–with-select_module
–without-select_module — 设置是否将select()方法模块编译进nginx中。如果系统平台不支持kqueue、epoll、rtsig或/dev/poll等更合适的方法, 该模块会被自动编译。
–with-poll_module
–without-poll_module — 设置是否将poll()方法模块编译进nginx中。如果系统平台不支持kqueue、epoll、rtsig或/dev/poll等更合适的方法, 该模块会被自动编译。
–without-http_gzip_module — 不编译http_gzip_module模块。该模块可以压缩HTTP服务器的响应,该模块需要zlib库才能编译和运行。
–without-http_rewrite_module — 不编译http_rewrite_module模块。该模块允许HTTP服务器重定向请求,改变请求的URI地址 。创建并运行该模块需要PCRE库支持。
–without-http_proxy_module — 不编译HTTP服务器的代理模块。
–with-http_ssl_module — 为HTTP服务器编译HTTPS协议支持的模块。该模块默认是不编译的。它需要OpenSSL库才能编译和运行。
–with-pcre=path — 设置PCRE库源文件的路径地址。PCRE库的发行版(version 4.4 — 8.30)需要先从PCRE站点下载并解压缩。 剩下的安装工作由nginx的./configure和make命令来完成。该库应用于location 指令的正则表达式支持以及ngx_http_rewrite_module模块。
–with-pcre-jit — 编译PCRE库时增加“实时编译(pcre_jit)”支持。
–with-zlib=path — 设置zlib库源文件的路径地址。zlib库的发行版(version 1.1.3 — 1.2.5)需要先从zlib站点下载并解压缩。 剩下的安装工作由nginx的./configure和make命令来完成。该库应用于 ngx_http_gzip_module模块。
–with-cc-opt=parameters — 设置将会添加额外参数到CFLAGS变量中。当在FreeBSD使用系统PCRE库时,需要指定 --with-cc-opt=“-I /usr/local/include”。 如果需要增加select()方法所支持的文件数量,也可以参照如下方式指定:
–with-cc-opt=“-D FD_SETSIZE=2048”。
–with-ld-opt=parameters — 设置将会在链接(linking)过程中使用的额外参数。当在FreeBSD使用系统PCRE库时,需要指定 --with-ld-opt=“-L /usr/local/lib”。
2.Nginx配置文件的详解
vi /usr/local/nginx/conf/nginx.conf
#nginx 启动用户 user nginx; #工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU数量 worker_processes 1; #错误日志:存放路径 error_log /var/log/nginx/error.log; # pid(进程标识符) 存放路径 pid logs/nginx.pid; #指定进程可以打开的最大描述符:数目 #这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit-n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。 #现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。 #这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。 worker_rlimit_nofile 204800; #工作模式即连接上限 events { #使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。 use epoll; #每个进程允许的最多连接数,根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。理论上每台nginx服务器的最大连接数为。worker_processes*worker_connections worker_connections 1024; #keepalive 超时时间 keepalive_timeout 60; #客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。 #分页大小可以用命令getconf PAGESIZE 取得。 #但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。 client_header_buffer_size 4k; #这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。 open_file_cache max=65535 inactive=60s; #这个是指多长时间检查一次缓存的有效信息。 open_file_cache_valid 80s; #open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。 open_file_cache_min_uses 1; } ##设定http服务器,利用它的反向代理功能提供负载均衡支持 http { #设定mime类型,类型由mime.type文件定义 include /etc/nginx/mime.types; default_type application/octet-stream; #日志格式设置。 #$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址; #$remote_user:用来记录客户端用户名称; #$time_local: 用来记录访问时间与时区; #$request: 用来记录请求的url与http协议; #$status: 用来记录请求状态;成功是200; #$body_bytes_sent :记录发送给客户端文件主体内容大小; #$http_referer:用来记录从那个页面链接访问过来的; #$http_user_agent:记录客户浏览器的相关信息; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径; #access_log logs/access.log main; #设定通过nginx上传文件的大小 client_max_body_size 300m; #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。 sendfile on; #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用 #tcp_nopush on; #keepalive_timeout 0; #连接超时时间 keepalive_timeout 65; # 开启gzip压缩 gzip on; #nginx的upstream目前支持4种方式的分配 #1、轮询(默认) #每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 #2、weight #指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 #例如: #upstream blog{ #server 192.168.0.14 weight=10; #server 192.168.0.15 weight=10; #} #2、ip_hash #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 #例如: #upstream blog{ #ip_hash; #server 192.168.0.14:88; #server 192.168.0.15:80; #} #3、fair(第三方) #按后端服务器的响应时间来分配请求,响应时间短的优先分配。 #upstream blog{ #server server1; #server server2; #fair; #} #4、url_hash(第三方) #按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 #例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法 #upstream blog { ##server squid1:3128; #server squid2:3128; #hash $request_uri; #hash_method crc32; #} ##upstream blog{#定义负载均衡设备的Ip及设备状态}{ #ip_hash; #server 127.0.0.1:9090 down; #server 127.0.0.1:8080 weight=2; #server 127.0.0.1:6060; #server 127.0.0.1:7070 backup; } #在需要使用负载均衡的server中增加 #proxy_pass http://blog/; #每个设备的状态设置为: #1.down表示单前的server暂时不参与负载 #2.weight为weight越大,负载的权重就越大。 #3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误 #4.fail_timeout:max_fails次失败后,暂停的时间。 #5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。 #nginx支持同时设置多组的负载均衡,用来给不用的server来使用。 #client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug #client_body_temp_path设置记录文件的目录 可以设置最多3层目录 #location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡 upstream blog { # weight 权重 被命中的概率 max_fails 熔断机制 连接超时次数超过 max_fails tomcat会被踢掉 server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s; server 127.0.0.1:9090 weight=1 max_fails=2 fail_timeout=30s; } # -------http请求入口------- server { # 监听端口号 80 listen 80; # 服务器域名 server_name www.CatGod007.com 007.com; # nginx 首页 index index.jsp index.html idex.htm; # nginx 资源文件路径 root /usr/local/nginx/html; #charset koi8-r; #access_log logs/host.access.log main; location / { # root html; # index index.html index.htm; 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_pass http://blog; } location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { # 动静分离 # 静态资源访问路径 root /usr/local/nginx/html; # 静态资源缓存到浏览器30天 expires 30d; } #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 ~/group([0-9])/M00 { root /data/fastdfs/data; ngx_fastdfs_module; } } server { listen 81; server_name haida.test.com; location / { root html; index index.html index.htm; } } server { listen 82; server_name nginx.test2.com; location / { root html; index index.html index.htm; } } # 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; # } #} }
感谢大家,点赞,收藏,关注,评论!