美文网首页Nginx高端成长之路Nginx 服务器
😝nginx 负载均衡 和反向代理配置

😝nginx 负载均衡 和反向代理配置

作者: 欧巴冰冰 | 来源:发表于2018-05-23 14:21 被阅读9次
  yum install -y nginx

通过yum安装的时候提示下面的错误

[root@localhost yum.repos.d]# yum install nginx
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
没有可用软件包 nginx。
错误:无须任何处理

需要添加nginx的源

[root@localhost yum.repos.d]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

该命令执行之后,会在/etc/yum.respos.d下面多出一个nginx.repo

[root@localhost yum.repos.d]# ll
总用量 40
-rw-r--r--. 1 root root 1572 12月  1 2016 CentOS-Base.repo
-rw-r--r--. 1 root root 1572 12月  1 2016 CentOS-Base.repo.backup
-rw-r--r--. 1 root root 1664 10月 24 10:36 CentOS-Base.repo.bak
-rw-r--r--. 1 root root 1309 8月  30 2017 CentOS-CR.repo
-rw-r--r--. 1 root root  649 8月  30 2017 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 8月  30 2017 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 8月  30 2017 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 8月  30 2017 CentOS-Sources.repo
-rw-r--r--. 1 root root 3830 8月  30 2017 CentOS-Vault.repo
-rw-r--r--. 1 root root  113 7月  15 2014 nginx.repo

然后再执行安装命令

yum install -y nginx

安装之后,可以查看nginx的默认安装目录

[root@localhost yum.repos.d]# whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
[root@localhost yum.repos.d]# pwd
/etc/yum.repos.d

以下是Nginx的默认路径:

(1) Nginx配置路径:/etc/nginx/

(2) PID目录:/var/run/nginx.pid

(3) 错误日志:/var/log/nginx/error.log

(4) 访问日志:/var/log/nginx/access.log

(5) 默认站点目录:/usr/share/nginx/html

事实上,只需知道Nginx配置路径,其他路径均可在/etc/nginx/nginx.conf 以及/etc/nginx/conf.d/default.conf 中查询到

nginx相关的验证命令及启动命令

[root@localhost yum.repos.d]# nginx                    
[root@localhost yum.repos.d]# nginx -t                   
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost yum.repos.d]# nginx -s reload             

nginx 启动

nginx -t 测试命令

nginx -s relaod 修改nginx.conf之后,可以重载

yum 安装参考:http://www.itmuch.com/install/nginx-yum-install-in-centos7/
编译方式安装参考 https://www.cnblogs.com/hafiz/p/6891458.html?utm_source=itdadao&utm_medium=referral


nginx 配置优化

/etc/nginx/nginx.conf

worker_processes  4; ##几个 CPU 就改为几
events {
    worker_connections  2048; 
}

负载均衡规则配置

/etc/nginx/conf.d/ default.conf


upstream blog-wap.xxx.com {
   #server 171.xx.x.xxx:8090 weight=1;
   #server 171.xx.x.xxx:8090 weight=1;
   server 171.xx.x.xxx:8090 weight=1;
   #server 171.xx.x.xxx:8090 weight=1;
   server 171.xx.x.xxx:8090 weight=1;
   server 171.xx.x.xxx:8090 weight=1;
   server 171.xx.x.xxx:8090 weight=1;
   server 171.xx.x.xxx:8090 weight=1;
}
upstream blog-api.xxx.com {
    #server 171.xx.x.xxx:8090 weight=1;
    #server 171.xx.x.xxx:8090 weight=1;
    server 171.xx.x.xxx:8090 weight=1;
    #server 171.xx.x.xxx:8090 weight=1;
    server 171.xx.x.xxx:8090 weight=1;
    server 171.xx.x.xxx:8090 weight=1;
    server 171.xx.x.xxx:8090 weight=1;
    server 171.xx.x.xxx:8090 weight=1;
}
upstream blog-test.xxx.com {
    server 171.xx.x.xxx:8090 weight=1;
}
upstream blog-admin.xxx.com {
    server 171.xx.x.xxx:8090 weight=1;
}
upstream blog-img.xxx.com {
    server 171.xx.x.xxx:8090 weight=1;
}
upstream njart-blog-src.xxx.com {
    server 171.xx.x.xxx:8090 weight=1;
}
upstream blog-dev.xxx.com {
    server 171.xx.x.xxx:8090 weight=1;
}

server {
    listen       80;
    server_name  blog-wap.xxx.com;
    location / {
        proxy_store off;
        proxy_redirect  off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass  http://blog-wap.xxx.com;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/njart/wap/webroot/index.html;
    }
}
server {
    listen       80;
    server_name  blog-api.xxx.com;
    location / {
        proxy_store off;
        proxy_redirect  off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass  http://blog-api.xxx.com;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/njart/wap/webroot/index.html;
    }
}
server {
    listen       80;
    server_name  blog-admin.xxx.com;
    location / {
        proxy_store off;
        proxy_redirect  off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass  http://blog-admin.xxx.com;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/njart/wap/webroot/index.html;
    }
}

##这台服务器, 支持接口代码访问,同时支持图片独立域名访问
server {
    listen       80;
    server_name  blog-img.xxx.com;
    location / {
        proxy_store off;
        proxy_redirect  off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass  http://blog-img.xxx.com;
    }
    location ~* \.(gif|jpg|jpeg|png|bmp)$ {
        proxy_store off;
        proxy_redirect  off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        #proxy_set_header Host $http_host;
        proxy_pass  http://njart-blog-src.xxx.com;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/njart/wap/webroot/index.html;
    }
}
server {
    listen       80;
    server_name  blog-dev.xxx.com;
    location / {
        proxy_store off;
        proxy_redirect  off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass  http://blog-dev.xxx.com;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/njart/wap/webroot/index.html;
    }
}
server {
    listen       80;
    server_name  blog-test.xxx.com;
    location / {
        proxy_store off;
        proxy_redirect  off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass  http://blog-test.xxx.com;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /data/web/njart/wap/webroot/index.html;
    }
}

相关文章

  • nginx

    nginx的配置、虚拟主机、负载均衡和反向代理一nginx的配置、虚拟主机、负载均衡和反向代理二nginx的配置、...

  • Nginx应用场景

    反向代理,负载均衡,动静分离 1.反向代理 修改nginx配置,并重新加载 重新加载nginx配置./nginx ...

  • 2020-03-31 Nginx反向代理与动静分离集群架构应用实

    1. Nginx负载均衡配置实战 1.1 Nginx负载均衡反向代理相关实践 1.1.1 实现为WWW服务代理 (...

  • nginx学习目录

    nginx安装部署和配置管理 nginx日志配置 nginx平滑升级与回滚 nginx反向代理 nginx负载均衡...

  • Nginx配置负载均衡

    说明 Nginx的主要用途就是用来配置反向代理和负载均衡的。反向代理可以参考https://www.jianshu...

  • Kong反向代理UPSTREAM配置

    Kong 配置upstream负载均衡 今天的目标是配置upstream(反向代理),实现nginx的最基础负载均...

  • 玩转nginx

    本文内容包括: nginx配置实例之反向代理; nginx配置实例之动静分离; nginx配置实例之负载均衡; n...

  • Nginx反向代理服务器+负载均衡

    nginx反向代理服务器+负载均衡 用nginx做反向代理和负载均衡非常简单, 支持两个用法 1个proxy, 1...

  • 负载均衡

    Nginx代理中的负载均衡 提到Nginx的反向代理,不得不提的就是Nginx的负载均衡,Nginx支持丰富的负载...

  • 4.常用配置

    反向代理 负载均衡 FastCGI 负载均衡详细配置

网友评论

本文标题:😝nginx 负载均衡 和反向代理配置

本文链接:https://www.haomeiwen.com/subject/cysrjftx.html