nginx的反向代理

== nginx ==
=== nginx 安装 ===
{{{
1.解压:
[root@localhost ~]# tar xzvf nginx-1.9.1.tar.gz
2.编译安装
[root@localhost nginx-1.9.1]# cd nginx-1.9.1
[root@localhost nginx-1.9.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
[root@localhost nginx-1.9.1]# make
[root@localhost nginx-1.9.1]# make install
}}}
=== nginx 启动/关闭 ===
{{{
1.启动
[root@localhost]# cd  /usr/local/nginx/sbin
[root@localhost sbin]# ./nginx
2.关闭
[root@localhost sbin]# ./nginx -s stop
当然你也可以找到这个nginx的进程,kill掉。
3.重启
[root@localhost sbin]# ./nginx -s reload
}}}
=== nginx 配置文件介绍 ===
{{{
nginx的默认配置文件是nginx.conf
[root@localhost conf]# cat nginx.conf

#user  nobody;  ------>代表运行nginx服务的用户,例如:user www www,分别是用户和用户组
worker_processes  1; ------->启动进程,通常设置成和cpu的数量相等

#error_log  logs/error.log; ------>全局错误日志
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;------>全局PID文件


events {
    worker_connections  1024;------>工作模式及连接数上限,即单个后台worker process进程的最大并发链接数
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
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;------>开启gzip压缩

    server {------>虚拟主机,如果有多个,就写多个server{...}
        listen       80;------>侦听80端口
        server_name  www.xx.com;------>定义使用www.xx.com访问

        #charset koi8-r;

        #access_log  logs/host.access.log  main; ------->设定本虚拟主机的访问日志

        #默认请求
        location / {
            root   html;------>定义服务器的默认网站根目录位置
            index  index.html index.htm;------>定义首页索引文件的名称
        }

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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;------>设置被代理的server的url,例如:https://192.168.99.198/
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;------>https server 默认监听的端口
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;------>公钥证书
    #    ssl_certificate_key  cert.key;------>私钥

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
}}}
=== nginx 反向代理配置 ===
{{{
这里以配置vGate为例,为vGate 设置https,http反向代理,每一个vGate 一个配置文件。

1.配置文件目录结构
创建一个conf.d目录,将所有用户的自定义配置文件放在这个目录下。
[root@localhost conf.d]#pwd
/usr/local/nginx/conf/conf.d

2.一个vGate的配置文件示例如下:
 server {
      listen       80;------>http默认监听的端口
      listen       443;------>https默认监听的端口
      server_name 1232131-3213213-fdfa-24r32434;------>这里是以openstack的租户id来作为域名的,你也可以使用其他域名。
        ssl on;
        #ssl_certificate      /usr/local/nginx/conf/ssl/vgate.com.crt; ------->这里的两个证书文件可在这里配置,也可以在nginx.conf里配置。
        #ssl_certificate_key  /usr/local/nginx/conf/ssl/vgate.com.key;
        location / {------>对/启动反向代理
         proxy_pass https://192.168.99.198/;------>代理的默认跟路径
        }

        location /log {
         proxy_pass https://192.168.99.198/;------>代理的日志页面路径
        }

        location /statistics {
         proxy_pass https://192.168.99.198/;------>代理的数据统计页面路径
        }

        location /policy {
         proxy_pass https://192.168.99.198/;------>代理的安全策略页面路径
        }
}

多个conf文件就写多个含有server 信息的文件,内容相差不大,如:
[root@localhost conf.d]# ls
1232131-3213213-fdfa-24r32434.conf  1232131-3213213-fdfa-24r32435.conf  1232131-3213213-fdfa-24r32436.conf  u1.conf  u2.conf

3.自定义的配置文件写完后,修改nginx主配置文件如下:
[root@localhost conf]# cat nginx.conf

#user  nobody;
user  www www;
worker_processes  4;

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

pid        logs/nginx.pid;


events {
     use epoll;
    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;
   root html;
   index index.html;
        server {
                listen 80;
                server_name localhost;

        }
    #gzip  on;
        ssl_certificate      /usr/local/nginx/conf/ssl/vgate.com.crt; ----------------->这里的证书文件需要利用openssl生成,然后在这里指定你的证书文件的路径即可,这个路径既可以写在每个自定义的conf中,也可以放在这里,然后其他所有的conf继承这个证书,就是所大家都用这一个证书
        ssl_certificate_key  /usr/local/nginx/conf/ssl/vgate.com.key;
   include conf.d/*.conf;------>把自定义的配置文件在nginx主配置文件中include一下即可
}

4.所有的配置文件创建并且修改完后,通过nginx本身提供的配置文件检测命令测试配置文件的正确性:
[root@localhost conf]# ../sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

或者:
[root@localhost conf]# ../sbin/nginx -t -c nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

5.重启nginx服务
[root@localhost conf]# ../sbin/nginx -s reload
6.浏览器访问
    6.1 通过域名访问时,要修改/etc/hosts文件,如下:
[root@localhost conf]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.99.197 1232131-3213213-fdfa-24r32436 1232131-3213213-fdfa-24r32435 1232131-3213213-fdfa-24r32434
其中的类似uuid的东西就是你在vgate conf中指定的server_name,192.168.99.197是nginx代理服务器的IP地址。
}}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值