当前位置: 首页> 科技> 互联网 > 龙口网页设计_安徽住房和城乡建设厅官网_软文营销的成功案例_企业网站制作教程

龙口网页设计_安徽住房和城乡建设厅官网_软文营销的成功案例_企业网站制作教程

时间:2025/7/12 7:38:13来源:https://blog.csdn.net/oxygen3000/article/details/142449475 浏览次数:0次
龙口网页设计_安徽住房和城乡建设厅官网_软文营销的成功案例_企业网站制作教程

前言

        Nginx是互联网应用的一层网关,是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。它最初是由俄罗斯的伊戈尔·赛索耶夫(Igor Sysoev)在2002年开发的,目的是为了解决C10K问题(即同时处理10000个客户端连接的问题)。Nginx以其高稳定性、丰富的功能集、简单的配置和低资源消耗而闻名。类似于apache,但Nginx更热门,版本迭代频繁,使用c语言写的,最主要的是它跨平台和易用性高。如下图可以直接看出,Nginx其实就是帮助我们整合多个服务于一体的一个助理,将所有服务联合起来,让用户看起来就想是一个应用程序一样。

        Nginx的使用非常简单,我们只需要修改配置文件就能用。在配置好的Nginx有一个conf文件,进入后可以看到nginx.conf。


#user  nobody;
worker_processes  3;#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 {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 {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;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 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;#    }#}}

        我们以后可以直接在这里面修改配置,然后执行命令./sbin/nginx -c conf/nginx.conf就可以启动nginx,在网页输入服务器ip,就可见如下界面。

        这个html也是在html文件里面的index.html。

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>

        我们查看nginx的进程,发现如下:

        由于在配置文件中我们只设置了一个work进程worker_processes  1;这里就是一个work进程。此外有个master进程。我们改一下配置,将work进程改为三个,可以看到三个work进程分别为36024、36025、36026。在配置文件中设置的监听端口是80,我们看80端口,只有master占用,那么所有的请求都由master进程处理。

        master进程首先读取配置文件、建立和初始化监听套接字,然后fork出work进程,但是多个进程监听同一个端口,就会出现惊群现象,也就是一个事件来了,这几个进程就都被唤醒。比如一个连接进来,多个进程都accept,但是服务器只会有一个进程真正处理,那么其他进程就是无效唤醒。当新的连接请求到来时,Nginx 的事件模型会将这个事件放入事件队列中。然后,Nginx 的事件分发机制会从队列中取出事件,并将其分配给一个工作进程,被分配到事件的工作进程会从监听套接字上接受连接,然后处理这个连接上的请求。Nginx使用epoll管理多个listenfd,当有新的连接请求到来时,操作系统会通知 Nginx,但不会指定具体是哪个工作进程来处理这个请求。

反向代理

        Nginx 的反向代理(Reverse Proxy)功能允许它将客户端的请求转发到一个或多个后端服务器,并将后端服务器的响应返回给客户端。这个过程对于客户端来说是透明的,客户端通常不知道它们实际上是与 Nginx 进行通信,而不是直接与后端服务器通信。所谓反向代理,就是客户端访问服务器,服务器被代理,正向代理就是客户端访问服务器,客户端被代理(翻墙)。

        咋用?在配置文件中定义一个 upstream 块,列出所有后端服务器的地址。这使得你可以集中管理后端服务器,并且可以轻松地添加或移除服务器。

upstream backend {server ip1:port;server ip2:port;server ip3:port;
}

        在 server 块中,你可以设置监听的端口(通常是 80 用于 HTTP 或 443 用于 HTTPS),并定义如何处理传入的请求。

server {listen 80;server_name example.com;location / {proxy_pass http://ip1:port;  # 将请求转发到定义的 upstream 块proxy_pass http://ip2:port;proxy_pass http://ip3:port;  }
}

        但上述是 Nginx 默认的负载均衡方法,按照时间顺序轮流将请求分发到不同的服务器。此外还有如下几种配置方法:

我们可以根据服务器的性能或负载能力分配不同的权重,权重越高的服务器将处理更多的请求。

upstream backend_servers {server backend1.example.com weight=3;server backend2.example.com weight=2;server backend3.example.com weight=1;
}

将请求分配给当前活动连接数最少的服务器,适合请求处理时间差异较大的场景。

upstream backend_servers {least_conn;server backend1.example.com;server backend2.example.com;server backend3.example.com;
}

可以设置失败尝试次数和失败后暂停服务的时间,以实现健康检查。

upstream backend_servers {server backend1.example.com max_fails=3 fail_timeout=30s;server backend2.example.com max_fails=3 fail_timeout=30s;server backend3.example.com max_fails=3 fail_timeout=30s;
}

        我们会发现,location后面的/是表示直接从根目录寻找,但如果所有的http请求都按照这种模式去寻找资源,性能肯定不是最优的,这时候我们就可设置多个locatiion,将/改为不同的,频繁的路径或目录,这样就可以更快找到资源,缓解压力。

关键字:龙口网页设计_安徽住房和城乡建设厅官网_软文营销的成功案例_企业网站制作教程

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: