nginx常用配置总结

一、修改配置文件

  • nginx的配置文件分为三部分,我们一般只需要关注http配置的部分即可
    • 基本配置
    • events配置
    • http配置
  • 配置文件详解
# =======基本配置=======================================
user nobody; # 配置work进程运行用户,即进程的名称,windows下可以注释掉
worker_processes 1; #配置工作进程数目,根据硬件调整,通常等于cpu数量或者2倍cpu数量
error_log logs/error.log; #配置全局错误日志及类型,[debug | info | notice | warn | eror | crit ] 默认是error
#pid logs/nginx.pid; # 配置进程pid文件
# =======events 配置=======================================
events {
    worker_connections 1024; #配置每个work进程连接数上限,nginx支持的总连接数等于worker_processes * worker_connections
}
# =======http 配置=======================================
http {
    include mime.types; #配置ngix支持哪些多媒体类型,可以在conf/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日志及存放路径,并使用上面定义的main日志格式
    #access_log logs/access.log main;

    sendfile on; #开启高效文件传输模式
    #tcp_nopush on; #防止网络阻塞

    #keepalive_timeout 0;
    keepalive_timeout 65; #长连接超时时间,单位是秒

    #gzip on; # 开启gzip压缩输出

    # 配置虚拟主机,可以多个
    server {
        listen 80; #监听端口
        server_name localhost; #服务器主机

        #charset koi8-r; #字符集(默认的是俄罗斯字符集)

        #access_log logs/host.access.log main; #日志路径,如果配置了就优先用这个,否则用公共的

        location / { #模糊匹配请求url里面带有斜杠的
            root html; #配置服务器的默认网站根目录位置,默认为nginx安装主目录下的html目录
            index index.html index.htm; # 配置文件首页名称,可以配置多个,如果第一个找不到会自动往后找
        }

        #error_page 404 /404.html; #404页面

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html; #其他报错页面配置
        location = /50x.html { # 精确匹配
            root html;
        }

        #php请求转发
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        # proxy_pass http://127.0.0.1;
        #}

        #php请求转发
        # 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;
        #}
    }
}

二、nginx的使用场景

nginx一般有两种常用场景,即作为静态资源网站和负载均衡

静态网站

nginx是一个HTTP的web服务器,可以将服务器上的静态文件(如html、图片等)通过http协议返回给浏览器客户端

sever {
    listen 80    #端口号
    location / {
        root /opt/www/; #静态文件路径
    }
}
负载均衡
  • 修改配置文件http部分
upstream www.baiducom {
    server 127.0.0.1:9100 weight=1;    //weight 是权重
    server 127.0.0.1:9100 weight=3;
}
location / {
    proxy_pass http://www.baiducom;    // 这儿的网址要和upstream后面的字符串一样
}

在这里插入图片描述

  • 注意:如果访问不了有可能是端口导致的,修改掉默认的80端口再试一下

三、常用的负载均衡策略

1、轮询(默认)
  • 每个请求轮流分配到不同的后端服务器,如果后端服务器down掉,将自动剔除
 upstream tomcatserver {
  server localhost:9090; 
  server localhost:8080;
 }
2、权重
  • 每个请求按一定比例分发到不同的后端服务器,weight值越大访问比例越大,用于后端服务器性能不均的情况
 upstream tomcatserver {
  server localhost:9090 weight=3; 
  server localhost:8080 weight=1;
 }
3、ip_hash
  • ip_hash也叫ip绑定,每个请求按方位ip的hash值分配,这样每个访问客户端会固定访问一个后端服务器,可以解决会话session丢失的问题
 upstream tomcatserver {
  ip_hash;
  server localhost:9090; 
  server localhost:8080;
 }
4、最少连接策略
  • web请求会被转发到连接数最少的服务器上
 upstream tomcatserver {
 least_conn;
  server localhost:9090; 
  server localhost:8080;
 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章