Nginx是一种高性能的HTTP和反向代理服务器,特点是占用内存少,并发能力强,可支持50000并发。
Nginx支持热部署,可以在不间断服务的情况下完成升级。
查看版本:./nginx -v
关闭:./ngnix -s stop
启动:./ngnix
重加载:读取新的配置文件:./ngnix -s reload
Nginx的配置文件可以分为三部分,分别是全局部分,event部分和HTTP部分。
HTTP块又可以分为HTTP全局块和server块。
#user nobody;
worker_processes 1; # 进程数,通常配置成与CPU核数相同#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024; # Ngnix支持的最大连接数
}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;# nginx支持轮询、权重、iphash的负载均衡方式,如果server后不接热河字段默认为轮询方式。upstream upstream_name {# ip_hash; # 如果有该字段,表示使用ip_hash进行负载均衡,相同ip会被hash到相同的server上。ip_hash可以与weight一起使用。# least_conn; # 最小链接数负载均衡,请求会被优先转发到连接数最少的server上# fair; # 根据响应时间进行负载均衡,请求会被转发到当前响应最快的服务器上server 127.0.0.1:8080 weight=2; # 每个server都是一个负载均衡的后端, weight字段代表权重server 127.0.0.1:9090 weight=1;}# 每一个server就是一个代理的配置server {listen 80;server_name localhost; # 对外提供的服务ip#charset koi8-r;#access_log logs/host.access.log main;location / { # 代理配置,每个location对应的是一个URI路径root html; # 用于访问静态资源,后面可以接资源路径proxy_pass http://127.0.0.1:8080; # 配置一个反向代理,将127.0.0.1:80的访问转发到127.0.0.1:8080# proxy_pass http://upstream_name; # 为当前请求使用一个负载均衡,通过name定位index index.html index.htm;}# 四种匹配方式,=代表严格匹配,~代表区分大小写的正则匹配,~*代表不区分大小写的正则匹配,^~表示正则匹配,且在匹配开头之后立即使用,不再继续搜索location [ = | ~ | ~* | ^~ | @ ] /edu { # ~代表后面是正则表达式,这部分表示匹配/edu结尾的URL,并将其转发到www.baidu.com上去,@用来处理内部重定向proxy_pass http://www.baidu.com;}#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 ~ /\.ht {# deny all;#}}# 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;# }#}}
简介:正向代理是在客户端无法直接访问目标网站时,通过代理服务器间接访问网站的过程。在访问时客户端是明确的知道代理服务器和目标服务器的地址的。
简介:反向代理是在服务端进行的。客户端访问的是反向代理服务器,代理服务器再将请求转发到实际服务器上。反向代理会隐藏实际的服务器地址。
反向代理需要在配置文件中的HTTP块中添加新的转发规则,具体步骤如下:
打开/usr/local/webserver/ngnix/conf目录下的ngnix.conf文件,在HTTP块中添加一个server块。
配置server块的listen端口和server_name 地址,这两个配置是该反向代理为客户端暴露的端口。
为server块添加一个location,用于匹配具体的URL。
在location中添加proxy_pass字段,将匹配成功的URL转发到新的地址。
配置负载均衡需要在配置文件中添加一个upstream块。upstream块在HTTP块内。
Nginx支持多种负载均衡方式:
将静态资源和动态资源分开存储,降低单个服务器的压力。CDN就是动静分离的一种应用,在CDN节点存储静态资源,如果需要动态资源再从源站获取。
上一篇:终于等到了!热刺门将维卡里奥上演意大利国家队处子秀 终于等到了!热刺门将维卡里奥上演意大利国家队处子秀
下一篇:文班:我手机上都没啥社交软件 有时我会删除几天几周甚至几个月 文班:我手机上都没啥社交软件 有时我会删除几天几周甚至几个月