使用场景

  • 静态资源服务
    • 通过本地文件系统提供服务
  • 反向代理
    • 强大的性能
    • 缓存
    • 负载均衡
  • API 服务
    • OpenResty

      优点

  1. 高并发,高性能
  2. 可扩展性好
  3. 高可靠性
  4. 支持热部署
  5. BSD 许可证,可二次开发并用于商用

运行文件组成

共由 4 部分组成,分别是:

  1. Nginx 二进制可执行文件

    服务的主体,由各个模块的源码编译出来的

  2. Nginx.conf 配置文件

    用来控制 Nginx 的行为

  3. access.log 访问日志

    记录一条 http 请求信息(请求、响应)

  4. error.log 错误日志

    用来定位问题

配置文件通用语法

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. server {
  23. listen 80;
  24. server_name localhost;
  25. #charset koi8-r;
  26. #access_log logs/host.access.log main;
  27. location / {
  28. root html;
  29. index index.html index.htm;
  30. }
  31. #error_page 404 /404.html;
  32. # redirect server error pages to the static page /50x.html
  33. #
  34. error_page 500 502 503 504 /50x.html;
  35. location = /50x.html {
  36. root html;
  37. }
  38. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  39. #
  40. #location ~ \.php$ {
  41. # proxy_pass http://127.0.0.1;
  42. #}
  43. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  44. #
  45. #location ~ \.php$ {
  46. # root html;
  47. # fastcgi_pass 127.0.0.1:9000;
  48. # fastcgi_index index.php;
  49. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  50. # include fastcgi_params;
  51. #}
  52. # deny access to .htaccess files, if Apache's document root
  53. # concurs with nginx's one
  54. #
  55. #location ~ /\.ht {
  56. # deny all;
  57. #}
  58. }
  59. # another virtual host using mix of IP-, name-, and port-based configuration
  60. #
  61. #server {
  62. # listen 8000;
  63. # listen somename:8080;
  64. # server_name somename alias another.alias;
  65. # location / {
  66. # root html;
  67. # index index.html index.htm;
  68. # }
  69. #}
  70. # HTTPS server
  71. #
  72. #server {
  73. # listen 443 ssl;
  74. # server_name localhost;
  75. # ssl_certificate cert.pem;
  76. # ssl_certificate_key cert.key;
  77. # ssl_session_cache shared:SSL:1m;
  78. # ssl_session_timeout 5m;
  79. # ssl_ciphers HIGH:!aNULL:!MD5;
  80. # ssl_prefer_server_ciphers on;
  81. # location / {
  82. # root html;
  83. # index index.html index.htm;
  84. # }
  85. #}
  86. }
  • 配置文件由指令和指令块组成
  • 每条指令以分号 ; 结尾
  • 指令与参数之间以空格隔开,几个空格无所谓
  • 指令块以大括号 {} 将多条指令组合在一起
  • include 允许组合多个配置文件,提高可维护性、可阅读性
  • 注释使用 # 在每行开头
  • $ 开头的是变量
  • 部分指令的参数支持正则表达式
  • 空间、容量配置时,不写单位默认是 bytes

http 指令块由 server、upstream、location 三个指令块组成。

运行时的进程结构

  1. [root@localhost nginx]# ps -ef | grep nginx
  2. root 20126 1 0 00:11 ? 00:00:00 nginx: master process ./sbin/nginx -c /home/xu-dev/nginx/conf/nginx.conf
  3. root 20132 20126 0 00:12 ? 00:00:00 nginx: worker process
  4. root 20133 20126 0 00:12 ? 00:00:00 nginx: worker process
  5. root 20134 20126 0 00:12 ? 00:00:00 nginx: worker process

Nginx 由两种进程组成,分别被称为 Master 进程和 Worker 进程;真正接收处理请求的是 Worker 进程,而 Master 进程负责管理 Worker 进程是否正常工作、是否需要重新载入配置文件、是否需要热部署……
多进程的方式保证了 Nginx 的高可靠性,那么为了在实际使用的时候能够最大化对硬件的使用率,我们通常会把 Worker 进程的数量配置成与 CPU 核心数一致,并且分别将每个进程绑定到每颗 CPU 上。

虚拟主机

利用虚拟主机,可以一台或一组 Nginx 服务器可以运行多个网站。
nginx.conf中,每个 server命令块代表一个虚拟主机。

  1. http {
  2. server {
  3. listen 80;
  4. location / {
  5. index index.html;
  6. }
  7. }
  8. }