Nginx介绍
Nginx安装
使用brew进行安装nginx,brew 又叫Homebrew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件, 只需要一个命令, 非常方便。
#列出已安装的软件$ brew list#更新brew$ brew update#用浏览器打开brew的官方网站$ brew home#显示包依赖$ brew deps#安装$ brew install example#查找$ brew search example#显示软件信息$ brew info#信息$ brew info nginx// 文件位置Docroot is: /opt/homebrew/var/www#安装$ brew install nginx
Nginx命令
#重新载入配置文件$ nginx -s reload#重启Nginx$ nginx -s reopen#强制停止Nginx$ nginx -s stop#查看端口占用netstat -lnp|grep 端口号#杀死所有nginx进程$ killall nginx#优雅地停止Nginx服务(即处理完所有请求后再停止服务)$ nginx -s quit#nginx运行状态$ ps -ef | grep nginx#检测配置文件是否有语法错误,然后退出$ nginx -t#设置配置文件(默认是:/etc/nginx/nginx.conf)$ nginx -c filename#设置配置文件外的全局指令$ nginx -g directives#设置前缀路径(默认是:/usr/share/nginx/)$ nginx -p prefix
Nginx配置文件
配置文件位置
/opt/homebrew/etc/nginx/nginx.conf
配置文件组成
- 全局块
从配置文件到enevts块之间的内容,主要会影响nginx服务器整体运行的配置指令,主要包括配置运行Nginx服务器的用户(组)、允许生成的worker process数,进程PID存放路径、日志存放路径和类型以及配置文件的引入等
例如第一行配置:
worker_processes 1;// 这是Nginx服务器并发处理的关键配置,worker_processes值越大,可以支持的的并发处理量也越多,但是会收到硬件、软件的引入等
- events块
events涉及的指令主要影响Nginx服务器与用户的网络连接,常用的设置包括是否开启对多work process下的网络连接进行序列化,是否允许同时接受多个网络连接,选取哪种事件驱动模型来处理连接请求,每个work process可以同时支持的最大连接数等。
例如:
worker_connections 1024// 支持的最大连接数
- http
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 {
#文件扩展名与文件类型映射表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 8080;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;# }#}include servers/*;
}
```javascriptlocation / {deny 192.168.1.100; // 禁止allow 192.168.1.10/200; // 允许allow 10.110.50.16;deny all; // 禁止所有}

