[Nginx]安装配置反向代理

  今天在腾讯云上的服务器因为没备案不让域名访问了,最开始想去做备案迁移,把阿里的备案迁移到腾讯了,但是发现求别人做事儿好麻烦,干脆自己弄个反向代理就好了,写个帖子纪录一下。

1.安装Ngix
2.Nginx命令
3.配置反向代理
4.配置负载均衡

  介绍一下我的环境:Linux系统Centos 64位   nginx-1.5.9  httpd2.1

1.先在阿里的服务器上安装Nginx。

这里是借鉴:点击这里 这篇文章安装的,里面有一个小小的问题,我等下到了那一步详细说。

第一步:从http://nginx.org/download/上下载相应的版本(或者wget http://nginx.org/download/nginx-1.5.9.tar.gz直接在Linux上用命令下载)

wget http://nginx.org/download/nginx-1.5.9.tar.gz

第二步:解压 tar -zxvf nginx-1.5.9.tar.gz

tar -zxvf nginx-1.5.9.tar.gz
cd nginx-1.5.9

第三步:设置一下配置信息 ./configure --prefix=/usr/local/nginx ,或者不执行此步,直接默认配置(这里没错,这是最后安装的路径)

./configure --prefix=/usr/local/nginx

第四步:

make 编译 (make的过程是把各种语言写的源码文件,变成可执行文件和各种库文件)

make install 安装 (make install是把这些编译出来的可执行文件和库文件复制到合适的地方)

make
make install

在配置信息的时候,也就是在第三步,出现了一下错误(我的环境没有报错,但我也同样贴出来,我的是集成环境):

错误为:./configure: error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel解决问题

yum -y install pcre-devel

还有可能出现:

错误提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.

解决办法:

yum -y install openssl openssl-devel 

2.安装后在linux下启动和关闭nginx:

启动操作(错误从这里开始,原文:/usr/nginx/sbin/nginx:,可能笔者这里路径没有问题,但是不太可能把,在configure的时候指向的是/usr/loacl/nginx)

/usr/local/nginx/sbin/nginx (/usr/local/nginx/sbin/nginx -t 查看配置信息是否正确)

停止操作
停止操作是通过向nginx进程发送信号(什么是信号请参阅linux文 章)来进行的
步骤1:查询nginx主进程号

ps -ef | grep nginx

在进程列表里 面找master进程,它的编号就是主进程号了。
步骤2:发送信号
从容停止Nginx:

kill -QUIT 主进程号

快速停止Nginx:

kill -TERM 主进程号

强制停止Nginx:

pkill -9 nginx

另外, 若在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文 件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送信号了,命令如下:

kill -信号类型 '/usr/local/nginx/logs/nginx.pid'

平滑重启
如果更改了配置就要重启Nginx,要先关闭Nginx再打开?不是的,可以向Nginx 发送信号,平滑重启。
平滑重启命令:
kill -HUP 住进称号或进程号文件路径
或者使用

/usr/local/nginx/sbin/nginx -s reload 

注意,修改了配置文件后最好先检查一下修改过的配置文件是否正 确,以免重启后Nginx出现错误影响服务器稳定运行。判断Nginx配置是否正确命令如下:

nginx -t -c /usr/local/nginx/conf/nginx.conf

或者

/usr/nginx/local/sbin/nginx -t

如果成功入下图片:

3 4.配置反向代理
   这部分我是参考:点击这里 这篇博客和我另外一个JAVA环境服务器上的Nginx的配置,我把我的贴出来把通配符的部分标注出来。

#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
  #这里我是从博客里看到这种格式写的,大概意思就是一个别名,这里的SERVER可以写多个,在这里加个参数就是负载均衡了(这只是最基础的配法)。
upstream tencnt { server 182.119.120.110:8989;
     #这里weight 表示优先级,导致的结果就是 112的访问量比111多一倍 注意分号
     #server 182.119.120.111:8899 weight=5;
     #server 182.119.120.112:8899 weight=10;
} server { listen 80;
     #名字可以修改对配置不会有影响
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main;      #匹配那些请求是被拦截的,tencnt 是上面配置的名字,你可以改成你自己的,我这里因为是链接上腾讯的服务器就写了这个,其他都比较好懂了,再下面的是一些默认配置,删除不会影响,从#Proxy Settings开始
        location ~ .* {
        proxy_pass  http://tencnt;
            root   html;
            index  index.html index.htm;

        #Proxy Settings
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

重启你的服务器看看OK了没有。

我写的这个能帮助你实现功能,仅供配置参考,深入学习你需要去看其他的资料。

感谢两位的博客:
http://www.cnblogs.com/kunhu/p/3633002.html
http://www.nowamagic.net/academy/detail/1226280

原文地址:https://www.cnblogs.com/flyingMonkey/p/5169858.html