1、nginx是如何实现高并发的?
答:一个主进程,多个工作进程,每个工作进程可以处理多个请求,每进来一个request,会有一个 worker 进程去处理。但不是全程的处理,处理到可能发生阻塞的地方,比如向上游(后端)服务器转发 request ,并等待请求返回。那么,这个处理的 worker 继续处理其他请求,而一旦上游服务器返回了,就会触发这个事件,worker 才会来接手,这个 request 才会接着往下走。由于 web server 的工作性质决定了每个 request 的大部份生命都是在网络传输中,实际上花费在 server 机器上的时间片不多。这是几个进程就解决高并发的秘密所在。即 @skoo 所说的 webserver 刚好属于网络 io 密集型应用,不算是计算密集型。
2、Nginx如何处理HTTP请求?
答:Nginx 使用反应器模式。主事件循环等待操作系统发出准备事件的信号,这样数据就可以从套接字读取,在该实例中读取到缓冲区并进行处理。单个线程可以提供数万个并发连接。
3、使用“反向代理服务器”的优点是什么?
答:反向代理服务器可以隐藏源服务器的存在和特征。它充当互联网云和 web 服务器之间的中间层。这对于安全方面来说是很好的,特别是当您使用 web 托管服务时。
4、列举Nginx服务器的最佳用途。
答:Nginx 服务器的最佳用法是在网络上部署动态 HTTP 内容,使用 SCGI、WSGI 应用程序服务器、用于脚本的 FastCGI 处理程序。它还可以作为负载均衡器。
5、Nginx服务器上的Master和Worker进程分别是什么?
答:Master 进程:读取及评估配置和维持 ;Worker 进程:处理请求。
6、什么是C10K问题?
答:C10K 问题是指无法同时处理大量客户端(10,000)的网络套接字。
7、请陈述stub_status和sub_filter指令的作用是什么?
答:Stub_status 指令:该指令用于了解 Nginx 当前状态的当前状态,如当前的活动连接,接受和处理当前读/写/等待连接的总数 ;
Sub_filter 指令:它用于搜索和替换响应中的内容,并快速修复陈旧的数据
8、为什么不使用多线程?
答:Nginx采用单线程来异步非阻塞处理请求(管理员可以配置 Nginx 主进程的工作进程的数量),不会为每个请求分配 cpu 和内存资源,节省了大量资源,同时也减少了大量的 CPU 的上下文切换,所以才使得 Nginx 支持更高的并发。
9、为什么要做动、静分离?
答:在我们的软件开发中,有些请求是需要后台处理的(如:.jsp,.do等等),有些请求是不需要经过后台处理的(如:css、html、jpg、js 等等),这些不需要经过后台处理的文件称为静态文件,否则动态文件。因此我们后台处理忽略静态文件,但是如果直接忽略静态文件的话,后台的请求次数就明显增多了。
在我们对资源的响应速度有要求的时候,应该使用这种动静分离的策略去解决动、静分离将网站静态资源(HTML,JavaScript,CSS 等)与后台应用分开部署,提高用户访问静态代码的速度,降低对后台应用访问。这里将静态资源放到 nginx 中,动态资源转发到 tomcat (opens new window)服务器中,毕竟 Tomcat 的优势是处理动态请求。
10、ngx_http_upstream_module的作用是什么?
答:要在 URL 中保留双斜线,就必须使用 merge_slashes_off;
语法:merge_slashes [on/off] ;
默认值: merge_slashes on;
环境: http,server
11、nginx配置文件路径
答:默认在/usr/local/nginx/conf下nginx.conf
12、nginx.conf常见配置
user root;
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 {
include mime.types;
default_type application/octet-stream;
client_max_body_size 150m;
#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 81;
server_name localhost;
charset utf-8;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /root/.jenkins/workspace/YwCloud-Front/ruoyi-ui/dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /prod-api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Proxy-Pass-Info http://192.168.10.112:7070;
proxy_pass http://localhost:7070/;
}
#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;
#}
}
server {
listen 85;
server_name localhost;
charset utf-8;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /home/application/sc/YwCloud-Front/ruoyi-ui/dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /prod-api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Proxy-Pass-Info http://www.dtsyw.cn:54321;
proxy_pass http://localhost:6060/;
}
#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;
# }
#}
}