反向代理是什么

server 根据请求中的信息,将请求分发到不同的地方,此 server 即为反向代理。

  1. 以 Nginx 配置多域名网站为例
  2. 以 server.js 转发请求为例

    nginx

  1. nginx能做什么?
  2. 正向代理(代理的是客户端),反向代理(代理的是服务端)负载均衡就是一个网站的内容被部署在若干服务器上,可以把这些机子看成一个集群那Nginx可以将接收到的客户端请求“均匀地”分配到这个集群中所有的服务器上,从而实现服务器压力的平均分配,也叫负载均衡。Nginx适配PC或移动设备($http_user_agent

IO多路复用
  1. 多个描述符的IO操作都能在一个线程里面并发交替完成
  1. select,线性遍历文件描述符列表。但效率低下,最多有1024个线程
  2. epoll模型,每当fd就绪,采用系统回调函数将fd放入。效率高,没有1024限制

CPU亲和

吧CPU核心和nginx工作进程绑定方式,每个进程固定在一个CPU上执行,减少切换CPU、提高缓存命中率

查看配置文件和目录

  1. rpm -ql nginx

基本命令:

  1. rpm -ql nginx 查看安装位置
  2. 启动:nginx,systemctl start nginx.service
  3. 停止: nginx -s quit, nignx -s stop, killall nginx
  4. 重启: systemctl restart nignx.service,
  5. 重载配置文件: nignx -s reload
  6. netstat -tlnp 查看开启的端口
server_name 监听的主机(ip或域名)
~ .php$ 正则匹配以.php结尾的路径
access_log 访问成功后的记录日志格式
~ ^/download 以/download开头的路径
2304

定义Nginx运行的用户和用户组

user nobody;

nginx进程数,建议设置为等于CPU总核心数。

worker_processes 1;

全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]

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 {

文件扩展名与文件类型映射表

include mime.types;

默认文件类型

default_type application/octet-stream;

  1. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  2. # '$status $body_bytes_sent "$http_referer" '
  3. # '"$http_user_agent" "$http_x_forwarded_for"';
  4. #access_log logs/access.log main;
  5. #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改 成off。
  6. sendfile on;
  7. #防止网络阻塞
  8. #tcp_nopush on;
  9. #长连接超时时间,单位是秒
  10. #keepalive_timeout 0;
  11. keepalive_timeout 65;
  12. #开启gzip压缩输出
  13. #gzip on;
  14. #虚拟主机的配置
  15. server {
  16. #监听端口
  17. listen 80;
  18. #域名可以有多个,用空格隔开
  19. server_name localhost;
  20. #默认编码
  21. #charset utf-8;
  22. #定义本虚拟主机的访问日志
  23. #access_log logs/host.access.log main;
  24. location / {
  25. root html; 网站的根目录
  26. index index.html index.htm;
  27. }
  28. #error_page 404 /404.html;
  29. # redirect server error pages to the static page /50x.html
  30. #
  31. error_page 500 502 503 504 /50x.html;
  32. location = /50x.html {
  33. root html;
  34. }
  35. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  36. #
  37. #location ~ \.php$ {
  38. # proxy_pass http://127.0.0.1;
  39. #}
  40. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  41. #
  42. #location ~ \.php$ {
  43. # root html;
  44. # fastcgi_pass 127.0.0.1:9000;
  45. # fastcgi_index index.php;
  46. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  47. # include fastcgi_params;
  48. #}
  49. # deny access to .htaccess files, if Apache's document root
  50. # concurs with nginx's one
  51. #
  52. #location ~ /\.ht {
  53. # deny all;
  54. #}
  55. }
  56. # another virtual host using mix of IP-, name-, and port-based configuration
  57. #
  58. #server {
  59. # listen 8000;
  60. # listen somename:8080;
  61. # server_name somename alias another.alias;
  62. # location / {
  63. # root html;
  64. # index index.html index.htm;
  65. # }
  66. #}
  67. # HTTPS server
  68. #
  69. #server {
  70. # listen 443 ssl;
  71. # server_name localhost;
  72. # ssl_certificate cert.pem;
  73. # ssl_certificate_key cert.key;
  74. # ssl_session_cache shared:SSL:1m;
  75. # ssl_session_timeout 5m;
  76. # ssl_ciphers HIGH:!aNULL:!MD5;
  77. # ssl_prefer_server_ciphers on;
  78. # location / {
  79. # root html;
  80. # index index.html index.htm;
  81. # }
  82. #}

}