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

来源:互联网 发布:c语言两竖线 编辑:程序博客网 时间:2024/06/05 08:44

由于Nginx的主要原理在其它的篇章内已经有所纪录,所以基本原理不再细述。


1. Centos 安装 Nginx

偷了个懒,直接用yum进行安装。当然如果是银行的业务,当然不能是如此简单就可以让你完成的。乖乖找到依赖的rpm包,写脚本安装吧。
当然在阿里云上,有网一切好说。基本操作如下所示:

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

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

[root@iZuf6gsbgu35znsy7v83s6Z ~]# yum info nginx已加载插件:fastestmirrorbase                                                                                                                                                          | 3.6 kB  00:00:00epel                                                                                                                                                          | 4.3 kB  00:00:00extras                                                                                                                                                        | 3.4 kB  00:00:00updates                                                                                                                                                       | 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:01Determining 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

随后安装

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;重启nginxsudo 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 startservice nginx stop/usr/sbin/nginx -s reload/usr/sbin/nginx -s stop
  • 阿里云和某些网址无法反向代理

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

    另有几个小细节:

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

    2. nginx怎么反向代理到百度


3. 参考

  1. nginx 入门、安装与简单使用
  2. nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol) 报错
  3. 阿里云OSS上使用自己的域名
  4. nginx怎么反向代理到百度
  5. yum安装nginx详解
  6. nginx服务器配置:nginx.pid” failed (2: No such file or directory
  7. Nginx 编译安装
  8. Unable to locate package错误解决办法

4. 删除和安装log日志

[root@iZuf6gsbgu35znsy7v83s6Z ~]# yum install nginx已加载插件:fastestmirrornginx                                                                                                                                                         | 2.9 kB  00:00:00nginx/x86_64/primary_db                                                                                                                                       |  24 kB  00:00:00Loading 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 MIs this ok [y/d/N]: yDownloading packages:nginx-1.12.0-1.el7.ngx.x86_64.rpm                                                                                                                             | 716 kB  00:00:03Running transaction checkRunning transaction testTransaction test succeededRunning 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/8warning: /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完毕!
原创粉丝点击