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

  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;
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,188评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,464评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,562评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,893评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,917评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,708评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,430评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,342评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,801评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,976评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,115评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,804评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,458评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,008评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,135评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,365评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,055评论 2 355

推荐阅读更多精彩内容