替换默认源

默认情况下是 HomeBrew,可以改为阿里云的源:

  1. # 替换brew.git:
  2. cd "$(brew --repo)"
  3. git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
  4. # 替换homebrew-core.git:
  5. cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
  6. git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
  7. # 应用生效
  8. brew update
  9. # 替换homebrew-bottles:
  10. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
  11. source ~/.zshrc

使用回官方源:

  1. # 重置brew.git:
  2. cd "$(brew --repo)"
  3. git remote set-url origin https://github.com/Homebrew/brew.git
  4. # 重置homebrew-core.git:
  5. cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
  6. git remote set-url origin https://github.com/Homebrew/homebrew-core.git
  7. # 删除zshrc HOMEBREW_BOTTLE_DOMAIN内容

安装 nginx

  1. brew install nginx

启动 nginx

  1. nginx
  2. brew services start nginx

启动成功后,打开 http://localhost:8080

文件目录

  • nginx安装文件目录
    /usr/local/Cellar/nginx
  • nginx配置文件目录
    /usr/local/etc/nginx
  • config文件目录
    /usr/local/etc/nginx/nginx.conf
  • 系统hosts位置
    /private/etc/hosts
  • 主页文件
    /usr/local/var/www

常用命令

  1. #启动nginx
  2. nginx
  3. #查看版本,以及配置文件地址
  4. nginx -V
  5. #查看版本
  6. nginx -v
  7. #重新加载配置|重启|快速停止|安全关闭nginx
  8. nginx -s reload|reopen|stop|quit
  9. #帮助
  10. nginx -h
  11. #查看nginx服务
  12. ps aux | grep nginx

卸载 nginx

  1. brew uninstall nginx

配置文件

/usr/local/etc/nginx/nginx.conf 文件是Nginx总配置文件:

  1. #运行用户,默认即是nginx,可以不进行设置
  2. user nginx;
  3. #Nginx进程,一般设置为和CPU核数一样
  4. worker_processes 1;
  5. #错误日志存放目录
  6. error_log /var/log/nginx/error.log warn;
  7. #进程pid存放位置
  8. pid /var/run/nginx.pid;
  9. events {
  10. worker_connections 1024; # 单个后台进程的最大并发数
  11. }
  12. http {
  13. include /etc/nginx/mime.types; #文件扩展名与类型映射表
  14. default_type application/octet-stream; #默认文件类型
  15. #设置日志模式
  16. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  17. '$status $body_bytes_sent "$http_referer" '
  18. '"$http_user_agent" "$http_x_forwarded_for"';
  19. access_log /var/log/nginx/access.log main; #nginx访问日志存放位置
  20. sendfile on; #开启高效传输模式
  21. #tcp_nopush on; #减少网络报文段的数量
  22. keepalive_timeout 65; #保持连接的时间,也叫超时时间
  23. #gzip on; #开启gzip压缩
  24. include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件

/usr/local/etc/nginx/nginx.conf.default 是一个子文件的配置项:

  1. server {
  2. listen 80; #配置监听端口
  3. server_name localhost; //配置域名
  4. #charset koi8-r;
  5. #access_log /var/log/nginx/host.access.log main;
  6. location / {
  7. root /usr/share/nginx/html; #服务默认启动目录
  8. index index.html index.htm; #默认访问文件
  9. }
  10. #error_page 404 /404.html; # 配置404页面
  11. # redirect server error pages to the static page /50x.html
  12. #
  13. error_page 500 502 503 504 /50x.html; #错误状态码的显示页面,配置后需要重启
  14. location = /50x.html {
  15. root /usr/share/nginx/html;
  16. }
  17. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  18. #
  19. #location ~ \.php$ {
  20. # proxy_pass http://127.0.0.1;
  21. #}
  22. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  23. #
  24. #location ~ \.php$ {
  25. # root html;
  26. # fastcgi_pass 127.0.0.1:9000;
  27. # fastcgi_index index.php;
  28. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  29. # include fastcgi_params;
  30. #}
  31. # deny access to .htaccess files, if Apache's document root
  32. # concurs with nginx's one
  33. #
  34. #location ~ /\.ht {
  35. # deny all;
  36. #}
  37. }

修改端口

修改 /usr/local/etc/nginx/nginx.conf

  1. http {
  2. server {
  3. listen 8181;
  4. server_name localhost;
  5. #charset koi8-r;
  6. .....
  7. }
  8. }
  9. }

配置 404 页面

修改配置:

  1. error_page 404 /404.html;
  2. error_page 404 http://baidu.com; #使用外部页面

在 usr/local/var/www/ 下新增 404.html

  1. <html>
  2. ...
  3. </html>

访问控制

服务器只允许特定主机访问。可以在 location 里配置。deny 表示禁止,allow 表示允许。在同一个块下的权限指令,匹配到就不会再继续匹配下一条规则了。

  1. location / {
  2. deny 123.4.32.3;
  3. allow 35.33.202.3;
  4. }

网站下的 img 可以被所有用户访问,但 admin 目录则只允许公司内部固定IP访问,需要 location 块来完成相关的需求匹配。

  1. location = /img {
  2. allow all
  3. }
  4. location = /admin {
  5. deny all;
  6. }

=号代表精确匹配,使用了=后是根据其后的模式进行精确匹配。也使用正则表达式设置访问权限。

  1. location ~\.php$ {
  2. deny all;
  3. }

设置虚拟主机

虚拟主机是指在一台物理主机服务器上划分出多个磁盘空间,每个磁盘空间都是一个虚拟主机,每台虚拟主机都可以对外提供Web服务,并且互不干扰。配置虚拟主机可以基于端口号、基于IP和基于域名。

基于端口号来配置虚拟主机,原理是 Nginx 监听多个端口,根据不同的端口号,来区分不同的网站。

在配置文件夹下新建一个配置文件 8001.conf ,作为子配置文件:

  1. server{
  2. listen 8001;
  3. server_name localhost;
  4. root /usr/local/var/www/html8001;
  5. index index.html;
  6. }
  7. # 监听 8001 端口,跟目录在 /usr/local/var/www/html8001 文件夹下,首页是 index.html

基于IP和基于端口的配置几乎一样,只是把server_name选项,配置成IP就可以了。

  1. server{
  2. listen 80;
  3. server_name 112.74.164.244;
  4. root /usr/local/var/www/html8001;
  5. index index.html;
  6. }

基于域名配置虚拟主机,把server_name选项,配置成域名就可以了。

  1. server{
  2. listen 80;
  3. server_name zq.com;
  4. root /usr/local/var/www/html8001;
  5. index index.html;
  6. }

反向代理

客户端发送的请求,内容被发送到代理服务器上,这个代理服务器再把请求发送到自己设置好的内部服务器上,而用户真实想获得的内容就在这些设置好的服务器上。代理服务器向外部客户端提供了一个统一的代理入口,客户端的请求都要先经过这个代理服务器。具体访问那个服务器是由 Nginx 来控制的。再简单点来讲,一般代理指代理的客户端,反向代理是代理的服务器。

使用反向代理用户只能通过外来网来访问代理服务器,并且用户并不知道自己访问的真实服务器是那一台,可以很好的提供安全保护。反向代理的主要用途是实现服务器压力的平均分配,为多个服务器提供负债均衡、缓存等功能。

  1. server{
  2. listen 80;
  3. server_name nginx.a.com;
  4. location / {
  5. proxy_pass http://a.com;
  6. }
  7. }
  8. # 将 nginx.a.com 代理到 a.com

反向代理还有些常用的指令:

  • proxy_set_header :在将客户端请求发送给后端服务器之前,更改来自客户端的请求头信息。
  • proxy_connect_timeout:配置Nginx与后端代理服务器尝试建立连接的超时时间。
  • proxy_read_timeout : 配置Nginx向后端服务器组发出read请求后,等待相应的超时时间。
  • proxy_send_timeout:配置Nginx向后端服务器组发出write请求后,等待相应的超时时间。
  • proxy_redirect :用于修改后端服务器返回的响应头中的Location和Refresh。

适配pc端和移动端

Nginx通过内置变量 $http_user_agent,可以获取到请求客户端的 userAgent,就可以用户目前处于移动端还是PC端,进而展示不同的页面给用户。

  1. server{
  2. listen 80;
  3. server_name nginx2.jspang.com;
  4. location / {
  5. root /usr/share/nginx/pc;
  6. if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
  7. root /usr/share/nginx/mobile;
  8. }
  9. index index.html;
  10. }
  11. }