nginx笔记

本文详细介绍了Nginx的作用,包括作为反向代理服务器,实现服务请求的转发,以及如何配置负载均衡策略,如轮询、加权轮询和IP哈希。此外,还讲解了Nginx的常用命令,如启动、停止和重新加载配置文件,并展示了如何配置HTTP服务器,包括动态和静态资源的分离以及设置反向代理。最后,提供了具体的Nginx配置示例,涉及服务器监听、错误日志、负载均衡配置及路径处理等。
摘要由CSDN通过智能技术生成

本文是观看狂神说nginx视频的笔记

一. 作用:

1 反向代理:

服务请求交由代理服务器去访问目标服务器,客户端可以通过代理服务器获取目标服务器资源。

2 负载均衡:

  1. 轮询:依次交替访问;

  2. 加权轮询:按权重比例依次交替访问。

  3. iphash:对客户端请求的ip进行hash操作,然后根据hash结果将同一个客户端ip的请求分发给同一台服务器进行处理,可以解决session不共享的问题(session共享使用redis是比较好的选择)。

  4. 动静分离:将服务器的动态资源和静态资源区分开来,然后将静态资源进行缓存,不需要通过jar包获取,从而提高相应速率。

二. Nginx常用命令

cd /安装路径
./nginx  启动
./nginx -s stop  停止
./nginx -s quit  安全退出(一步一步回收已结束的线程)
./nginx -s reload  重新加载配置文件(每次修改完nginx配置文件都需要执行)
ps aux|grep nginx  查看nginx进程

三. Nginx配置相关

# ---- 全局配置 start ----
#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;
# ---- 全局配置 end ----
​
# ---- 策略相关 start ----
events {
    # 最大连接数
    worker_connections  1024;
}
# ---- 策略相关 end ----
​
# ---- http相关 ----
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;
​
    # 负载均衡配置 domain_name可以任取
    upstream domain_name{
        server 192.168.5.77:8080 weight=1;
        server 192.168.5.77:8081 weight=3;
    }
​
    server {
        listen       80;
        server_name  localhost;
​
        #charset koi8-r;
​
        #access_log  logs/host.access.log  main;
​
        # 路径配置 /:表示根目录
        location / {
            root   html;
            index  index.html index.htm;
            # 反向代理
            proxy_pass  http://domain_name;
        }
        
        location /admin {
            root   html;
            index  index.html index.htm;
            # 反向代理
            proxy_pass  http://domain_name;
        }
​
        #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;
    #    }
    #}
​
}
​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值