Nginx介绍

image.png

Nginx安装

使用brew进行安装nginx,brew 又叫Homebrew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件, 只需要一个命令, 非常方便。

  1. #列出已安装的软件
  2. $ brew list
  3. #更新brew
  4. $ brew update
  5. #用浏览器打开brew的官方网站
  6. $ brew home
  7. #显示包依赖
  8. $ brew deps
  9. #安装
  10. $ brew install example
  11. #查找
  12. $ brew search example
  13. #显示软件信息
  14. $ brew info
  15. #信息
  16. $ brew info nginx
  17. // 文件位置Docroot is: /opt/homebrew/var/www
  18. #安装
  19. $ brew install nginx

Nginx命令

  1. #重新载入配置文件
  2. $ nginx -s reload
  3. #重启Nginx
  4. $ nginx -s reopen
  5. #强制停止Nginx
  6. $ nginx -s stop
  7. #查看端口占用
  8. netstat -lnp|grep 端口号
  9. #杀死所有nginx进程
  10. $ killall nginx
  11. #优雅地停止Nginx服务(即处理完所有请求后再停止服务)
  12. $ nginx -s quit
  13. #nginx运行状态
  14. $ ps -ef | grep nginx
  15. #检测配置文件是否有语法错误,然后退出
  16. $ nginx -t
  17. #设置配置文件(默认是:/etc/nginx/nginx.conf)
  18. $ nginx -c filename
  19. #设置配置文件外的全局指令
  20. $ nginx -g directives
  21. #设置前缀路径(默认是:/usr/share/nginx/)
  22. $ nginx -p prefix

Nginx配置文件

配置文件位置

  1. /opt/homebrew/etc/nginx/nginx.conf

配置文件组成

  1. 全局块

从配置文件到enevts块之间的内容,主要会影响nginx服务器整体运行的配置指令,主要包括配置运行Nginx服务器的用户(组)、允许生成的worker process数,进程PID存放路径、日志存放路径和类型以及配置文件的引入等
例如第一行配置:

  1. worker_processes 1;
  2. // 这是Nginx服务器并发处理的关键配置,worker_processes值越大,可以支持的的并发处理量也越多,但是会收到硬件、软件的引入等
  1. events块

events涉及的指令主要影响Nginx服务器与用户的网络连接,常用的设置包括是否开启对多work process下的网络连接进行序列化,是否允许同时接受多个网络连接,选取哪种事件驱动模型来处理连接请求,每个work process可以同时支持的最大连接数等。
例如:

  1. worker_connections 1024
  2. // 支持的最大连接数
  1. http
  1. 配置文件示例 ```javascript

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

    user nobody;

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

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 {

  1. #文件扩展名与文件类型映射表
  2. include mime.types;
  3. #默认文件类型
  4. default_type application/octet-stream;
  5. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  6. # '$status $body_bytes_sent "$http_referer" '
  7. # '"$http_user_agent" "$http_x_forwarded_for"';
  8. #access_log logs/access.log main;
  9. sendfile on;
  10. #tcp_nopush on;
  11. #keepalive_timeout 0;
  12. keepalive_timeout 65;
  13. #gzip on;
  14. server {
  15. listen 8080;
  16. server_name localhost;
  17. #charset koi8-r;
  18. #access_log logs/host.access.log main;
  19. location / {
  20. root html;
  21. index index.html index.htm;
  22. }
  23. #error_page 404 /404.html;
  24. # redirect server error pages to the static page /50x.html
  25. #
  26. error_page 500 502 503 504 /50x.html;
  27. location = /50x.html {
  28. root html;
  29. }
  30. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  31. #
  32. #location ~ \.php$ {
  33. # proxy_pass http://127.0.0.1;
  34. #}
  35. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  36. #
  37. #location ~ \.php$ {
  38. # root html;
  39. # fastcgi_pass 127.0.0.1:9000;
  40. # fastcgi_index index.php;
  41. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  42. # include fastcgi_params;
  43. #}
  44. # deny access to .htaccess files, if Apache's document root
  45. # concurs with nginx's one
  46. #
  47. #location ~ /\.ht {
  48. # deny all;
  49. #}
  50. }
  51. # another virtual host using mix of IP-, name-, and port-based configuration
  52. #
  53. #server {
  54. # listen 8000;
  55. # listen somename:8080;
  56. # server_name somename alias another.alias;
  57. # location / {
  58. # root html;
  59. # index index.html index.htm;
  60. # }
  61. #}
  62. # HTTPS server
  63. #
  64. #server {
  65. # listen 443 ssl;
  66. # server_name localhost;
  67. # ssl_certificate cert.pem;
  68. # ssl_certificate_key cert.key;
  69. # ssl_session_cache shared:SSL:1m;
  70. # ssl_session_timeout 5m;
  71. # ssl_ciphers HIGH:!aNULL:!MD5;
  72. # ssl_prefer_server_ciphers on;
  73. # location / {
  74. # root html;
  75. # index index.html index.htm;
  76. # }
  77. #}
  78. include servers/*;

}

  1. ```javascript
  2. location / {
  3. deny 192.168.1.100; // 禁止
  4. allow 192.168.1.10/200; // 允许
  5. allow 10.110.50.16;
  6. deny all; // 禁止所有
  7. }

Nginx反向代理

image.png