绵竹移动网站建设,wordpress邮箱修改,建设培训网站建设,处理事件seo软件一.nginx常用命令1.Windows(1).查看nginx的版本号nginx -v(2).启动nginxstart nginx(3).快速停止或关闭nginxnginx -s stop(4).正常停止或关闭nginxnginx -s quit(5).配置文件nginx.conf修改重装载命令nginx -s reload2.Linux(1).进入 nginx 目录中cd /usr/local/nginx/sbin(2)…一.nginx常用命令1.Windows(1).查看nginx的版本号nginx -v(2).启动nginxstart nginx(3).快速停止或关闭nginxnginx -s stop(4).正常停止或关闭nginxnginx -s quit(5).配置文件nginx.conf修改重装载命令nginx -s reload2.Linux(1).进入 nginx 目录中cd /usr/local/nginx/sbin(2).查看 nginx 版本号./nginx -v(3).启动 nginx./nginx(4).停止 nginx./nginx -s stop(5).重新加载 nginx./nginx -s reload二.Nginx配置文件(nginx.conf)1.概述默认在Linux上安装的Nginx配置文件在安装的nginx目录下的conf目录下名字叫做nginx.confnginx.conf主要由三部分组成全局块 events块http块2.配置文件结构图解3.配置文件概览# 全局快------------------------------------------------------------------------------
#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块
events {worker_connections 1024;
}
------------------------------------------------------------------------------ # http块
http {#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;
------------------------------------------------------------------------------ # server块server {
------------------------------------------------------------------------------ # server全局块listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;----------------------------------------------------------------------------# location块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 Apaches document root# concurs with nginxs one##location ~ /\.ht {# deny all;#}}# 可以配置多个server块
}... #全局块events { #events块...
}http #http块
{... #http全局块server #server块{ ... #server全局块location [PATTERN] #location块{...}location [PATTERN] {...}}server{...}... #http全局块
}3.1全局块就是配置文件从头开始到events块之间的内容主要设置的是影响nginx服务器整体运行的配置指令一般有运行nginx服务器的用户组nginx进程pid存放路径日志存放路径配置文件引入允许生成worker process数等比如worker_process, 值越大可以支持的并发处理量也越多但是还是和服务器的硬件相关3.2 events块events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接常用的设置包括每个进程的最大连接数是否开启对多 work process下的网络连接进行序列化是否允许同时接收多个网络连接选取哪种事件驱动模型来处理连接请求每个 word process 可以同时支持的最大连接数等。上述例子就表示每个 work process 支持的最大连接数为 1024.注意这部分的配置对 Nginx 的性能影响较大在实际中应该灵活配置3.3 http块包括http全局块以及多个server块可以嵌套多个server配置代理缓存日志定义等绝大多数功能和第三方模块的配置。如文件引入mime-type定义日志自定义是否使用sendfile传输文件连接超时时间单连接请求数等3.3.1 http全局块http 全局块配置的指令包括文件引入、 MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。3.3.2 server块配置虚拟主机的相关参数一个http中可以有多个server这块和虚拟主机有密切关系虚拟主机从用户角度看和一台独立的硬件主机是完全一样的该技术的产生是为了节省互联网服务器硬件成本。每个 http 块可以包括多个 server 块而每个 server 块就相当于一个虚拟主机每个 server 块也分为全局 server 块以及可以同时包含多个 location 块3.3.2.1 server全局块最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或 IP 配置。#这一行表示这个server块监听的端口是80只要有请求访问了80端口此server块就处理请求
listen 80;
#表示这个server块代表的虚拟主机的名字
server_name localhost;3.3.2.2 location块配置请求的路由以及各种页面的处理情况一个 server 块可以配置多个 location 块.要作用是根据请求地址路径的匹配匹配成功进行特定的处理,这块的主要作用是基于 Nginx 服务器接收到的请求字符串例如 server_name/uri-string对虚拟主机名称也可以是 IP 别名之外的字符串例如 前面的 /uri-string进行匹配对特定的请求进行处理。地址定向、数据缓存和应答控制等功能还有许多第三方模块的配置也在这里进行# 表示如果请求路径是/就是用这个location块进行处理
location / {root html;index index.html index.htm;
}4.案例讲解########### 每个指令必须有分号结束。#################
#user administrator administrators; #配置用户或者组默认为nobody nobody。
#worker_processes 2; #允许生成的进程数默认为1
#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
error_log log/error.log debug; #制定日志路径级别。这个设置可以放入全局块http块server块级别以此为debug|info|notice|warn|error|crit|alert|emerg
events {accept_mutex on; #设置网路连接序列化防止惊群现象发生默认为onmulti_accept on; #设置一个进程是否同时接受多个网络连接默认为off#use epoll; #事件驱动模型select|poll|kqueue|epoll|resig|/dev/poll|eventportworker_connections 1024; #最大连接数默认为512
}
http {include mime.types; #文件扩展名与文件类型映射表default_type application/octet-stream; #默认文件类型默认为text/plain#access_log off; #取消服务日志 log_format myFormat $remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for; #自定义格式access_log log/access.log myFormat; #combined为日志格式的默认值sendfile on; #允许sendfile方式传输文件默认为off可以在http块server块location块。sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值默认为0即不设上限。keepalive_timeout 65; #连接超时时间默认为75s可以在httpserverlocation块。upstream mysvr { server 127.0.0.1:7878;server 192.168.10.121:3333 backup; #热备}error_page 404 https://www.baidu.com; #错误页server {keepalive_requests 120; #单连接请求上限次数。listen 4545; #监听端口server_name 127.0.0.1; #监听地址 location ~*^.$ { #请求的url过滤正则匹配~为区分大小写~*为不区分大小写。#root path; #根目录#index vv.txt; #设置默认页proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表deny 127.0.0.1; #拒绝的ipallow 172.18.5.54; #允许的ip } }
}5.注意事项(1).$remote_addr 与 $http_x_forwarded_for 用以记录客户端的ip地址(2).$remote_user 用来记录客户端用户名称(3).$time_local 用来记录访问时间与时区(4).$request 用来记录请求的url与http协议(5).$status 用来记录请求状态成功是200(6).$body_bytes_s ent 记录发送给客户端文件主体内容大小(7).$http_referer 用来记录从那个页面链接访问过来的(8).$http_user_agent 记录客户端浏览器的相关信息(9).惊群现象一个网路连接到来多个睡眠的进程被同时叫醒但只有一个进程能获得链接这样会影响系统性能(10).每个指令必须有分号结束6.常用配置案例6.1文件上传大小http { # 设置nginx文件上传大小限制 client_max_body_size 200M; client_body_buffer_size 50M; fastcgi_intercept_errors on;
}6.2 http 转 https方式①http端口server { rewrite ^(.*)$ https://$host$1 permanent;
}方式②同时http、https端口 server {listen 80;listen 443 ssl; server_name localhost;if ($scheme http) {return 301 https://$host$request_uri; }
}redirect重定向https跳转http问题报错“400 Bad Request: The plain HTTP request was sent to HTTPS port”解决方法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_set_header X-Forwarded-Proto https;
proxy_pass http://172.17.x.xxx:8081;
proxy_redirect http:// https://;实现流程是根据nginx的不同执行阶段来完成Location http到https1proxy_pass执行前先设置了request head host 为https外网访问的域名端口2proxy_pass执行后tomcat结果返回response3proxy_redirect修改response中的location中的协议http为https外网访问的协议。注redirect重定向主要是通过访问nginx服务的请求head项来决定的默认是http协议域名是通过读取host地址默认host中不包括访问端口方式③错误页从定向server {listen 8001 ssl; server_name localhost; error_page 497 https://$host:8009$uri?$args;
}6.3 隐藏版本号http { server_tokens off;
} 6.4.负载均衡upstreamupstream xxx.com { ip_hash; server 192.168.8.11:80; server 192.168.8.12:80 down; server 192.168.8.13:8009 max_fails3 fail_timeout20s; server 192.168.8.146:8080;
}注意当负载调度算法为ip_hash时后端服务器在负载均衡调度中的状态不能是weight和backupupstream是Nginx的HTTP Upstream模块这个模块通过一个简单的调度算法来实现客户端IP到后端服务器的负载均衡在上面的设定中通过upstream指令指定了一个负载均衡器的名称xxx.com这个名称可以任意指定在后面需要的地方直接调用即可。Nginx的负载均衡模块目前支持4种调度算法下面进行分别介绍其中后两项属于第三方的调度方法轮询默认每个请求按时间顺序逐一分配到不同的后端服务器如果后端某台服务器宕机故障系统被自动剔除使用户访问不受影响Weight指定轮询权值Weight值越大分配到的访问机率越高主要用于后端每个服务器性能不均的情况下ip_hash每个请求按访问IP的hash结果分配这样来自同一个IP的访客固定访问一个后端服务器有效解决了动态网页存在的session共享问题fair比上面两个更加智能的负载均衡算法。此种算法可以依据页面大小和加载时间长短智能地进行负载均衡也就是根据后端服务器的响应时间来分配请求响应时间短的优先分配。Nginx本身是不支持fair的如果需要使用这种调度算法必须下载Nginx的upstream_fair模块url_hash按访问url的hash结果来分配请求使每个url定向到同一个后端服务器可以进一步提高后端缓存服务器的效率。Nginx本身是不支持url_hash的如果需要使用这种调度算法必须安装Nginx 的hash软件包。在HTTP Upstream模块中可以通过server指令指定后端服务器的IP地址和端口同时还可以设定每个后端服务器在负载均衡调度中的状态。常用的状态有down表示当前的server暂时不参与负载均衡backup预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候才会请求backup机器因此这台机器的压力最轻max_fails允许请求失败的次数默认为1。当超过最大次数时返回proxy_next_upstream 模块定义的错误fail_timeout在经历了max_fails次失败后暂停服务的时间。max_fails可以和fail_timeout一起使用6.5 禁止文件缓存开发环境可使用location ~* \.(js|css|png|jpg|gif)$ {add_header Cache-Control no-store;
}6.6 防盗链防止文件被其他网站使用location ~* \.(gif|jpg|png)$ {# 只允许 192.168.0.1 请求资源 valid_referers none blocked 192.168.0.1;if ($invalid_referer) { rewrite ^/ http://$host/logo.png; }
}6.7文件压缩server {# 开启gzip 压缩 gzip on;# 设置gzip所需的http协议最低版本 HTTP/1.1, HTTP/1.0 gzip_http_version 1.1;# 设置压缩级别压缩级别越高压缩时间越长 1-9 gzip_comp_level 4;# 设置压缩的最小字节数 页面Content-Length获取 gzip_min_length 1000;# 设置压缩文件的类型 text/html) gzip_types text/plain application/javascript text/css;
}6.8 指定定错误页面# 根据状态码返回对于的错误页面
error_page 500 502 503 504 /50x.html;
location /50x.html { root /source/error_page;
}6.9解决跨域现在http://xx_domain对https://github.com发起请求一定会出现跨域。## 配置反向代理的参数
server {listen 8080; server_name xx_domain## 1. 用户访问 http://xx_domain则反向代理到 https://github.com location / { proxy_pass https://github.com; proxy_redirect off; proxy_set_header Host $host; # 传递域名 proxy_set_header X-Real-IP $remote_addr; # 传递ip proxy_set_header X-Scheme $scheme; # 传递协议 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
}6.10其他server { # 监听端口 HTTPS listen 443 ssl; server_name xxx.com; # 配置域名证书 ssl_certificate C:\WebServer\Certs\certificate.crt; ssl_certificate_key C:\WebServer\Certs\private.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers ALL:!ADH:!EXPORT56:RC4RSA:HIGH:MEDIUM:LOW:SSLv2:EXP; ssl_prefer_server_ciphers on; index index.html index.htm index.php; root /data/www/; location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } # 配置地址拦截转发解决跨域验证问题 location /oauth/ { proxy_pass https://localhost:13580/oauth/; proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
}