您的位置:首页 > 运维架构 > 反向代理

阿里云上安装Nginx及反向代理配置

2017-06-08 00:03 796 查看
由于Nginx的主要原理在其它的篇章内已经有所纪录,所以基本原理不再细述。

1. Centos 安装 Nginx

偷了个懒,直接用yum进行安装。当然如果是银行的业务,当然不能是如此简单就可以让你完成的。乖乖找到依赖的rpm包,写脚本安装吧。

当然在阿里云上,有网一切好说。基本操作如下所示:

# 查询 yum库内的 nginx信息
yum info nginx


如果有的话会显示如下信息。

[root@iZuf6gsbgu35znsy7v83s6Z ~]# yum info nginx
已加载插件:fastestmirror
base                                                                                                                                                          | 3.6 kB  00:00:00
epel                                                                                                                                                          | 4.3 kB  00:00:00
extras                                                                                                                                                        | 3.4 kB  00:00:00
updates                                                                                                                                                       | 3.4 kB  00:00:00
(1/7): epel/x86_64/group_gz                                                                                                                                   | 170 kB  00:00:00
(2/7): base/7/x86_64/group_gz                                                                                                                                 | 155 kB  00:00:00
(3/7): epel/x86_64/updateinfo                                                                                                                                 | 808 kB  00:00:00
(4/7): extras/7/x86_64/primary_db                                                                                                                             | 167 kB  00:00:00
(5/7): epel/x86_64/primary_db                                                                                                                                 | 4.8 MB  00:00:00
(6/7): base/7/x86_64/primary_db                                                                                                                               | 5.6 MB  00:00:01
(7/7): updates/7/x86_64/primary_db                                                                                                                            | 5.6 MB  00:00:01
Determining fastest mirrors
可安装的软件包
名称    :nginx
架构    :x86_64
时期       :1
版本    :1.10.2
发布    :1.el7
大小    :505 k
源    :epel/x86_64
简介    : A high performance web server and reverse proxy server
网址    :http://nginx.org/
协议    : BSD
描述    : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
: IMAP protocols, with a strong focus on high concurrency, performance and low
: memory usage.


没有的话,导入yum源:

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm[/code] 
随后安装

yum install -y nginx


随后 查看安装信息

nginx -v


随后启动

service nginx start
# 本人 nginx -c nginx.conf 启动出现一些问题


修改default.conf文件重启Nginx,修改内容如下:

server {
listen       80;
server_name  localhost;

#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

location / {
## 注释如下2行,添加第三行内容
#    root   /usr/share/nginx/html;
#    index  index.html index.htm;
proxy_pass http://blog.csdn.net/u010416101; }

location /csdn {
proxy_pass http://blog.csdn.net/u010416101; }

}


2. Tips

错误 “Unable to locate package错误解决办法”

说明yum库内没有nginx的rpm安装包,加载到yum库内即可。

错误”nginx: [emerg] open() “/etc/nginx/nginx” failed (2: No such file or directory)”和”**.access.log”

因为nginx启动时候 会临时生成这几个文件,一般是没有启动nginx,直接reload导致的。

错误”nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol) 报错”

处理办法

sudo vim /usr/local/nginx/conf/nginx.conf

修改

listen       80 default_server;
listen       [::]:80 default_server;

修改为:

listen       80;
#listen       [::]:80 default_server;

重启nginx
sudo service nginx restart


配置文件位置

启动脚本一般在/usr/sbin/nginx 脚本

默认配置文件(一般会修改它)在 /etc/nginx/conf.d/default.conf

自定义Nginx站点配置文件存放目录/etc/nginx/conf.d/

全局配置文件(一般不会去修改它)在 /etc/nginx/nginx.conf

网站默认站点配置 /etc/nginx/conf.d/default.conf

查看nginx是否启动

查看是否有其进程 ps -ef|grep nginx

杀死 nginx 进程 kill -9 pid /service nginx stop

启/停 Nginx

service nginx start
service nginx stop

/usr/sbin/nginx -s reload
/usr/sbin/nginx -s stop


阿里云和某些网址无法反向代理

因为阿里的域名(国有特色) 需要绑定 CNAME 才可以代理。具体怎么绑定看阿里官方文档。

另有几个小细节:

阿里云OSS上使用自己的域名

nginx怎么反向代理到百度

3. 参考

nginx 入门、安装与简单使用

nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol) 报错

阿里云OSS上使用自己的域名

nginx怎么反向代理到百度

yum安装nginx详解

nginx服务器配置:nginx.pid” failed (2: No such file or directory

Nginx 编译安装

Unable to locate package错误解决办法

4. 删除和安装log日志

[root@iZuf6gsbgu35znsy7v83s6Z ~]# yum install nginx
已加载插件:fastestmirror
nginx                                                                                                                                                         | 2.9 kB  00:00:00
nginx/x86_64/primary_db                                                                                                                                       |  24 kB  00:00:00
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 nginx.x86_64.1.1.12.0-1.el7.ngx 将被 安装
--> 解决依赖关系完成

依赖关系解决

=====================================================================================================================================================================================
Package                                 架构                                     版本                                                 源                                       大小
=====================================================================================================================================================================================
正在安装:
nginx                                   x86_64                                   1:1.12.0-1.el7.ngx                                   nginx                                   716 k

事务概要
=====================================================================================================================================================================================
安装  1 软件包

总下载量:716 k
安装大小:2.5 M
Is this ok [y/d/N]: y
Downloading packages:
nginx-1.12.0-1.el7.ngx.x86_64.rpm                                                                                                                             | 716 kB  00:00:03
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
正在安装    : 1:nginx-1.12.0-1.el7.ngx.x86_64                                                                                                                                  1/1
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/ 
Commercial subscriptions for nginx are available on:
* http://nginx.com/products/ 
----------------------------------------------------------------------
验证中      : 1:nginx-1.12.0-1.el7.ngx.x86_64                                                                                                                                  1/1

已安装:
nginx.x86_64 1:1.12.0-1.el7.ngx

完毕!


[root@iZuf6gsbgu35znsy7v83s6Z nginx]# yum remove nginx
已加载插件:fastestmirror
设置移除进程
解决依赖关系
--> 执行事务检查
---> Package nginx.x86_64 0:1.10.2-1.el6 will be 删除
--> 处理依赖关系 nginx,它被软件包 nginx-mod-http-perl-1.10.2-1.el6.x86_64 需要
--> 处理依赖关系 nginx,它被软件包 nginx-mod-http-xslt-filter-1.10.2-1.el6.x86_64 需要
--> 处理依赖关系 nginx,它被软件包 nginx-mod-mail-1.10.2-1.el6.x86_64 需要
--> 处理依赖关系 nginx,它被软件包 nginx-mod-http-image-filter-1.10.2-1.el6.x86_64 需要
--> 处理依赖关系 nginx,它被软件包 nginx-mod-stream-1.10.2-1.el6.x86_64 需要
--> 处理依赖关系 nginx,它被软件包 nginx-mod-http-geoip-1.10.2-1.el6.x86_64 需要
--> 执行事务检查
---> Package nginx-mod-http-geoip.x86_64 0:1.10.2-1.el6 will be 删除
--> 处理依赖关系 nginx-mod-http-geoip = 1.10.2-1.el6,它被软件包 nginx-all-modules-1.10.2-1.el6.noarch 需要
---> Package nginx-mod-http-image-filter.x86_64 0:1.10.2-1.el6 will be 删除
---> Package nginx-mod-http-perl.x86_64 0:1.10.2-1.el6 will be 删除
---> Package nginx-mod-http-xslt-filter.x86_64 0:1.10.2-1.el6 will be 删除
---> Package nginx-mod-mail.x86_64 0:1.10.2-1.el6 will be 删除
---> Package nginx-mod-stream.x86_64 0:1.10.2-1.el6 will be 删除
--> 执行事务检查
---> Package nginx-all-modules.noarch 0:1.10.2-1.el6 will be 删除
--> 完成依赖关系计算

依赖关系解决

=====================================================================================================================================================================================
软件包                                                    架构                                 版本                                       仓库                                 大小
=====================================================================================================================================================================================
正在删除:
nginx                                                     x86_64                               1.10.2-1.el6                               @epel                               1.3 M
为依赖而移除:
nginx-all-modules                                         noarch                               1.10.2-1.el6                               @epel                               0.0
nginx-mod-http-geoip                                      x86_64                               1.10.2-1.el6                               @epel                                17 k
nginx-mod-http-image-filter                               x86_64                               1.10.2-1.el6                               @epel                                21 k
nginx-mod-http-perl                                       x86_64                               1.10.2-1.el6                               @epel                                53 k
nginx-mod-http-xslt-filter                                x86_64                               1.10.2-1.el6                               @epel                                20 k
nginx-mod-mail                                            x86_64                               1.10.2-1.el6                               @epel                                96 k
nginx-mod-stream                                          x86_64                               1.10.2-1.el6                               @epel                                73 k

事务概要
=====================================================================================================================================================================================
Remove        8 Package(s)

Installed size: 1.6 M
确定吗?[y/N]:y
下载软件包:
运行 rpm_check_debug
执行事务测试
事务测试成功
执行事务
正在删除   : nginx-mod-http-geoip-1.10.2-1.el6.x86_64                                                                                                                          1/8
正在删除   : nginx-mod-http-image-filter-1.10.2-1.el6.x86_64                                                                                                                   2/8
正在删除   : nginx-mod-http-perl-1.10.2-1.el6.x86_64                                                                                                                           3/8
正在删除   : nginx-mod-http-xslt-filter-1.10.2-1.el6.x86_64                                                                                                                    4/8
正在删除   : nginx-mod-stream-1.10.2-1.el6.x86_64                                                                                                                              5/8
正在删除   : nginx-all-modules-1.10.2-1.el6.noarch                                                                                                                             6/8
正在删除   : nginx-mod-mail-1.10.2-1.el6.x86_64                                                                                                                                7/8
正在删除   : nginx-1.10.2-1.el6.x86_64                                                                                                                                         8/8
warning: /etc/nginx/nginx.conf saved as /etc/nginx/nginx.conf.rpmsave
Verifying  : nginx-mod-mail-1.10.2-1.el6.x86_64                                                                                                                                1/8
Verifying  : nginx-mod-http-geoip-1.10.2-1.el6.x86_64                                                                                                                          2/8
Verifying  : nginx-mod-stream-1.10.2-1.el6.x86_64                                                                                                                              3/8
Verifying  : nginx-mod-http-perl-1.10.2-1.el6.x86_64                                                                                                                           4/8
Verifying  : nginx-all-modules-1.10.2-1.el6.noarch                                                                                                                             5/8
Verifying  : nginx-mod-http-image-filter-1.10.2-1.el6.x86_64                                                                                                                   6/8
Verifying  : nginx-1.10.2-1.el6.x86_64                                                                                                                                         7/8
Verifying  : nginx-mod-http-xslt-filter-1.10.2-1.el6.x86_64                                                                                                                    8/8

删除:
nginx.x86_64 0:1.10.2-1.el6

作为依赖被删除:
nginx-all-modules.noarch 0:1.10.2-1.el6                nginx-mod-http-geoip.x86_64 0:1.10.2-1.el6                    nginx-mod-http-image-filter.x86_64 0:1.10.2-1.el6
nginx-mod-http-perl.x86_64 0:1.10.2-1.el6              nginx-mod-http-xslt-filter.x86_64 0:1.10.2-1.el6              nginx-mod-mail.x86_64 0:1.10.2-1.el6
nginx-mod-stream.x86_64 0:1.10.2-1.el6

完毕!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: