怎样做彩票网站,网站服务器用哪个好,房产cms,2023年中国500强榜单Nginx是什么#xff1f; Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件#xff08;IMAP/POP3#xff09;代理服务器。其特点是占有内存少#xff0c;并发能力强#xff0c;事实上nginx的并发能力确实在同类型的网页服务器中表现较好#xff0c;中国大陆使用ng… Nginx是什么 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件IMAP/POP3代理服务器。其特点是占有内存少并发能力强事实上nginx的并发能力确实在同类型的网页服务器中表现较好中国大陆使用nginx网站用户有百度、京东、新浪、网易、腾讯、淘宝等。 2 知识剖析 2.1 nginx服务器有什么作用 1、反向代理 2、负载均衡 3、动静分离 2.2 什么叫反向代理 反向代理反向代理ReverseProxy是指以代理服务器来接受internet上的连接请求然后将请求转发给内部网络上的服务器并将从服务器上得到的结果返回给internet上请求连接的客户端,简单来说就是真实的服务器不能直接被外部网络访问,想要访问必须通过代理。 2.3 为什么要使用反向代理 1、防止主服务器被恶意攻击 2、为负载均衡和动静分离提供实现支持 2.4 什么是负载均衡负载均衡的作用是什么 负载均衡就是将任务分摊到多个操作单元上进行执行。对于Nginx而言就是将收到的访问请求分发给不同的Web服务器以提高访问性能以及可靠性。负载均衡可以有效防止一个服务器宕机而导致服务停止。 当一台服务器的单位时间内的访问量越大时服务器压力就越大大到超过自身承受能力时服务器就会崩溃。为了避免服务器崩溃让用户有更好的体验我们通过负载均衡的方式来分担服务器压力。我们可以建立很多很多服务器组成一个服务器集群当用户访问网站时先访问一个中间服务器在让这个中间服务器在服务器集群中选择一个压力较小的服务器然后将该访问请求引入该服务器。如此一来用户的每次访问都会保证服务器集群中的每个服务器压力趋于平衡分担了服务器压力避免了服务器崩溃的情况。 配置负载均衡 http://nginx.org/en/docs/http/load_balancing.html 2.5 什么是动静分离动静分离的作用 动静分离运用Nginx的反向代理功能分发请求所有动态资源的请求交给应用服务器而静态资源的请求例如图片、视频、CSS、JavaScript文件等则直接由Nginx返回到浏览器 动静分离的作用主要是nginx处理静态页面的效率远高于tomcat的处理能力使用c语言开发的nginx对静态资源每秒的吞吐量是使用Java语言开发的tomcat的6倍也远高于其它应用服务器 3 常见问题 3.1 如何配置反向代理 通过配置实现 nginx的主配置文件名叫 nginx.config
它在nginx的安装目录下的config目录下面 复制代码
#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 { worker_connections 1024;
} #http块主要有三个作用域, http server location
http { #可以嵌套多个server ,配置代理,缓存,日志,等绝大多数功能,和第三方模块配置 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; # 开启高效文件传输模式,这个指令,指定了nginx是否调用sendfile函数传输文件,对于普通的应用设为on ,如果用来进行下载等IO重负载应用可设为off sendfile on; #tcp_nopush on; #防止网络阻塞 #keepalive_timeout 0; keepalive_timeout 65; # 长连接超时时常,单位秒 #gzip on; # 负载均衡
# upstream XXX.com{
# upstream负载均衡块, weight表示权重, 值越大,被分配到的几率就越大
# server 192.168.80.121:80 weight3;
# server 192.168.80.121:80 weight3;
# server 192.168.80.121:80 weight3;
#} #server配置虚拟主机的相关参数 server { listen 80; # 监听端口 server_name localhost; # 监听地址,可以采用域名加多个空格隔开,如果匹配不到我们在浏览器输入的域名.默认走 localhost, 然后location到nginx的欢迎页 #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; #} } # 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; # } #} }
复制代码
我们要实现反向代理就是要 新增我们自己的server
复制代码
server { listen 80; server_name 浏览器可能访问的域名1; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / { proxy_pass http://127.0.0.1:9001; # 转发路径 proxy_connect_timeout 600; proxy_read_timeout 600; } } server { listen 80; server_name 浏览器可能访问的域名2; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy