Nginx知识点总结

发布时间:2022-11-12 10:30

常用命令

./nginx 启动
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload 重新加载配置

基本配置解析

user  root;

#工作线程数,最佳配为当前主机的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 记录了文件后缀名对应的处理类型
    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;
    # 连接超时时间 单位是s
    keepalive_timeout  65;

    #gzip  on;

    server {
    	# 监听端口
        listen       80;
        # 域名、主机名
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

		# /是域名之后跟的内容
        location / {
        	# 资源路径 html是相对路径
            root   html;
            # 默认加载该路径下的文件名
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        # 出现以下错误码 跳转到/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;
    #    }
    #}

}

nginx路径匹配规则

先精准,后头通配符,再尾通配符,最后正则

反向代理

location / {
	# 反向代理到http://www.baidu.com,但不支持https
	proxy_pass http://www.baidu.com
}

负载均衡

负载均衡策略

  • 轮询
    默认情况下使用的方式,逐一转发,适用于无状态请求,如有session认证的就不行
  • ip_hash
    根据客户端的ip地址转发同一台服务器,可以保持会话,但有些如移动端切换网络ip变化就不适合了
    默认是根据ip进行hash,也可以指定hash的内容
    如根据请求地址进行hash: hash $request_uri;
    根据cookie里的jsessionid进行hash: hash $cookie_jsessionid;
  • least_conn
    最少连接访问
  • url_hash
    根据用户访问的url定向转发请求
  • fair
    根据后端服务器响应时间转发请求,需要下载插件

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号