nginx编译安装及配置初窥

nginx 编译安装

nginx.org download 源码包

根据需要安装对应的模块及设置相关路径,可参考nginx.org官方安装notes

  

 1 ~]# yum groupinstall "Development Tools" "Server Platform Development"
 2 ~]# yum install pcre-devel openssl-devel zlib-devel
 3 ~]# useradd -r nginx
 4 ~]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio
 5 # make && make install
 6 
 7 注:编译安装完成后需手动创建nginx用户及指定配置文件PATH;
 8 useradd -s /sbin/nologin -M -R nginx
 9 nginx -c /etc/nginx/nginx.conf
10 nginx -s reopen (重新打开nginx后端口已监听)
11 nginx命令PATH:/usr/local/nginx/sbin/nginx,需要将该命令加入PATH变量中;

配置文件的格式:

  由指令加value组成,并且每条指令由 ; 号结尾;

  可使用变量,分为nginx模块自带变量及自定义变量 set value_name value 

/etc/nginx/nginx.conf

http {
        log_format common '$remote_addr - $remote_user $time_local "$request" $s
tatus $body_bytes_sent "$http_referer" "$http_user_agent"';   (自定义日志格式名称及内容)

#       pid /var/run/nginx.pid;   (指定nginxpid文件存放的位置)
#       user nginx nginx;    (配置启动nginx woker进程的用户及组)
#       loca_module /etc/nginx/module;   (配置待加载模块文件路径)
#       include /etc/nginx/conf.d/;   (指定包含的配置文件路径)
#       worker_processes 4;   (指定启动的子进程数量,一般情况下配置为CPU的核心数量)
#       worker_cpu_affinity auto;   (指定CPU与nginx woker进程之间的对应绑定关系,一般为auto)
#       worker_priority 19;   (指定nginx_worker进程运行的nice优先级 -20~19 对应 100~139 ,19最高)
#       worker_rlimit_nofile 65535;   (指定nginx进程允许访问的最大文件数量,最高为65536)
#       daemon off;   (指定是否以守护进程方式运行,一般用于调试时使用)
#       master_process on;   (指定是否以 master/worker 方式工作,默认为On)
#       error_log /var/log/nginx/error_log;   (指明error_log file path)
#       events {   (事件驱动相关配置)
#               worker_connections 10240;  (配置每个worker进程最多可响应的请求数,nginx最大访问量为 worker_process*worker_connection)
#               use epoll;   (指定IO的调度方式,一般为epoll)
#               accept_mutex off;   (指定worker处理新请求的连接方式,off为将新请求发送到所有worker,on为顺序指派方式)
    }
server { listen
192.168.20.243:80 default_server backlog=1024 rcvbuf=100m sndbuf=100m; (指定监听的端口, default_server:配置为默认的响应服务 backlog:配置nginx请求排队队列最大数 rcvbuf:接受报文缓冲去大小 sndbuf:发送报文缓冲区大小 listen 192.168.20.243:443 ssl; : server_name www.ilinuc.com   (指定服务名称) tcp_nodelay off;   (指定是否将多个小的资源请求组合为一个大的报文统一发送,一般为off,开启后客户端访问显示速率可能会慢) tcp_nopush on;   (指定是否将报文头部与内容一起发送) sendfile on;   (指定是否开启当请求资源由磁盘调度到内核后,直接由内核发送至协议栈,不经过应用服务程序) root /date/vhosts;   (指定URL路径映射根目录) location / { allow 192.168.15.0/24; (设置URL根目录访问权限) deny all; } location /videos { (通过location 修改指定路径的根目录,当client access /videos ,左侧的根目录转换为/date/vhosts1目录;此时访问的是/date/vhosts1下的videos目录;) root /date/vhosts1/; allow 192.168.15.0/24; } location /admin {   (指定访问别名,当client访问/admin/目录时,右侧的根目录内容为/date/vhosts1/目录下的内容;) alias /date/vhosts2/; allow 192.168.15.0/24; deny all; } index index.html index.php index.html;   (当client未指定具体的访问路径时,反馈给客户端的主业页面文件;) error_page 404 /notfound.html;   (自定义指定错误代码的返回页面文件及路径) location /notfound.html {   (指明错误代码文件的位置) root /date/code/; allow 192.168.15.0/24; deny all; } location /index.html {    try_files $uri /huawei.html @finish;  (当client请求的page不存在时,尝试查找其它指定的page反馈给客户端,此处try 指定优先查找客户端请求的内容,不存在时查找/huawei.html反馈给客户端,不存在时查找@finish定义的路径; } location @finish {     (定义 @finish 的查找路路径) try_files /cisco.html =399;   (此处为当 /cisco.html文件不存在时,反馈 399错误给客户端;) } keepalive_timeout 65s;   (配置客户端连接超时时间) keepalive_requests 256;   (配置每个客户端连接最多可请求的page资源数量,到达上限或超过指定连接时常会自动断开该此连接,重新建立新的连接请求) keepalive_disable msie6;   (指定不允许保持连接的浏览器) send_timeout 60s;   (向客户端发送两次报文之间的超时时常,是指两次写操作之间的间隔时常) client_body_buffer_size 16k;   (指定客户端请求报文body部分在内存中允许缓存的大小;) client_body_temp_path /date/nginx/temp/ 2 1 1;   (指定客户端body部分超出内存允许大小时,内容存储的磁盘路径位置,2 1 1 为指定的存储目录结构,2为一级子目录,依次类推,可允许最多子目录为一级子目录*二级子目录*三级子目录;) limit_rate 4096;   (指定单个客户端访问速率,单位为 bytes) location ~ /.*$ {    limit_except GET {   (指定允许客户端使用的请求方式,对于指定的方式之外的请求方法不允许使用,一般不允许客户端使用delete,等危险请求操作) allow 192.168.15.0/24; deny all; : } } aio on;   (指定异步io工作模式是否开启) directio 1m;   (向client发送的资源大小大于指定的size时,请求内容直接由内核发送到协议栈,不经过应用层,此项与sendfile相同) open_file_cache max=256 inactive=60s;   (指定允许缓存已经读取过的文件的文件描述符及文件元数据信息(下次访问时通过缓存的访问路径直接读取date)最大缓存条目,及超时活动时常) open_file_cache_valid 30;   (指定检查缓存条目的时间间隔,单位为s) open_file_cache_min_uses 1;   (配置当 在inactive指定的超时时间内,通过每检查时间间隔内进行检查时匹配到的缓存条目被访问次数,当次数低于指定值,则将该条目从缓存项中删除) open_file_cache_errors on;   (配置是否开启缓存错误日志) # rewrite /(.*)$ https://www.ilinuc.com/$1;   (配置页面重定向,此处为当client访问URL路径下所有内容,重定向到https://www.ilinux.com/$1 页面,$1为( )内的页面路径) location /auth {   (配置访问指定路径时,需要使用用户认证) auth_basic "This is manager directory ,please input your grant"; auth_basic_user_file /etc/nginx/.passwd;   (指定存储用户账户文件的路径,用户账户一般使用 htpasswd 工具生成) } location /basic_status {   (指定当client访问basic_status时,反馈stub_status内容到client,stub_status为nginx服务器当前运行状态信息) stub_status; } access_log /var/log/nginx/access_log common buffer=8m flush=60s;   (指定访问日志文件路径及日志格式及日志缓冲先区大小等配置) location /log {   (配置为指定的URL路径使用单独的日志文件) access_log /var/log/nginx/log_access common; } gzip on;   (是否开启响应报文资源压缩,优势:节省带宽, 劣势:消耗主机CPU资源) gzip_comp_level 6;   (配置压缩层级 1~9 ,9级最高) gzip_min_length 2m;   (配置压缩响应报文大小的阀值,超出该大小的报文进行压缩) gzip_buffers 32 4k;   (支持实现压缩功能时为其配置的缓冲区的数量及每个缓冲区的大小;) gzip_proxied any;   (nginx作为代理服务器,接收到后端响应报文时,在何种情况下启用压缩功能,any为所有) gzip_types text/css text/xml application/javascript;   (支持压缩的资源文件格式;) ssl_certificate /etc/pki/CA/nginx.crt;   (当站点为https时使用,指明站点经CA签发的公有证书的文件路径) ssl_certificate_key /etc/pki/CA/private/cakey.pem;   (指定产生公有证书的私钥文件路径) ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv3;   (支持的加密协议) ssl_session_cache shared:ssl:10M;   (指定用于保存https会话的缓存区模式及模式名称和大小 shared为各worker进程共享一个缓冲区,ssl为内存下缓冲区名称,10M为缓冲区大小;) ssl_session_timeout 65s;   (https会话超时时常) # rewrite /(.*)$ https://www.ilinux.com/$1;   (重定向页面) location /referer {   (配置允许的客户端请求报文中携带的referer内容(通过哪个站点跳转到本页面) valid_referers www.ilinuc.com; if ($invalid_referer) {   (当client报文中 referer内容无法与给定的匹配项匹配成功,则nginx机制会为变量 $invalid_referer赋值为1,此处if语句为当变量$invalid_referer值为1时,停止客户端访问并反馈指定错误代码;) return 402;     (该功能可用于防止非法盗用图片及盗用链接行为) } } } } }

  

猜你喜欢

转载自www.cnblogs.com/qingfengguoke/p/12445641.html