Nginx教程

Mac 使用 nginx

本文主要是介绍Mac 使用 nginx,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

nginx 安装

brew install nginx :安装nginx
brew list nginx : 查看nginx 的安装目录
nginx -t :查看nginx 安装目录
/opt/homebrew/Cellar/nginx/1.21.0/bin/nginx -c /opt/homebrew/etc/nginx/nginx.conf : nginx 启动命令
安装目录 -c 指定配置文件启动

nginx 配置的基本解读

#user  nobody;
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;
    
    #负载均衡配置
     upstream localhost {
      #ip_hash; 
      server localhost:8180 weight=2;
      server localhost:8280;
    }
    
    server {
        listen       80; #nginx 对外暴露的端口
        server_name  localhost;#nginx 对外暴露的域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            proxy_pass  http://localhost; //请求代理的域名
            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/*;
}

nginx 负载的策略

参数配置说明:

fail_timeout与max_fails结合使用
max_fails设置在fail_timeout参数设置的时间内最大失败次数,如果在这个时间内,所有针对该服务器的请求都失败了,那么认为该服务器会被认为是停机了
fail_time服务器会被认为停机的时间长度,默认为10s
backup标记该服务器为备用服务器。当主服务器停止时,请求会被发送到它这里
down标记服务器永久停机了

负载的策略

1.轮询策略

 upstream localhost {
      server localhost:8180;
      server localhost:8280;
    }

对配置的负载,依次向它们进行请求的分发(调用的时候两次进行一次请求位置的更换???是配置的问题吗)

2:weight

 upstream localhost {
      server localhost:8180 weight=2;
      server localhost:8280;
    }

权重,默认为1,数字越大,权重越高,权重表示请求被分发到这个服务器上的概率

remark: 适用于服务器之间性能相差较多的情况

3: ip_hash

 upstream localhost {
      ip_hash; 
      server localhost:8180 weight=2;
      server localhost:8280;
    }

对每个请求的ip 进行hash取值,通过对应的范围请求对应的负载服务器,可以保证同一台机器的多次访问均由同一台服务器进行处理

remark:backup 不能与其共用

4: least_conn 最小连接

 upstream localhost {
      least_conn; 
      server localhost:8180 weight=2;
      server localhost:8280;
    }

当负载的服务器资源使用相差较多时,可以保证请求分发在资源占用较少的服务器上

这篇关于Mac 使用 nginx的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!