码迷,mamicode.com
首页 > 其他好文 > 详细

nginx反代

时间:2016-10-08 21:02:16      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:nginx反代

需求:同一主机上跑多个web站点,使用httpd+php-fpm无法满足需要;

解决方案:加一个nginx,接入所有用户访问,根据用户访问的主页不同,反代后端不同站点;

前端:nginx

后端:

同一主机多个web站点

www.bbtw.net

www.shxinsheng.wang

new.khcm.net




安装过程略,主要描述反代过程:

nginx主配置文件:

]# egrep -v ‘^[[:space:]]*#|^$‘ /etc/nginx/nginx.conf

user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {

    worker_connections 1024;

}

http {

    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  /var/log/nginx/access.log  main;

    sendfile            on;

    tcp_nopush          on;

    tcp_nodelay         on;

    keepalive_timeout   65;

    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;

    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {

        listen       80 default_server;

        listen       [::]:80 default_server;

        server_name  _;

        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;

        location / {

        }

        error_page 404 /404.html;

            location = /40x.html {

        }

        error_page 500 502 503 504 /50x.html;

            location = /50x.html {

        }

    }

}


采用每个站点一个配置文件,放在/etc/nginx/conf.d/目录下:

www.bbtw.net站点,配置文件为bbt.conf;

www.shxinsheng.wang站点,配置文件为xyk.conf

new.khcm.net站点,配置文件为khcm.conf


]# cat /etc/nginx/conf.d/bbt.conf 

server {

    listen       80;

    server_name www.bbtw.net;



    port_in_redirect   on;


keepalive_timeout  15;


location / {


proxy_set_header Connection ‘‘;

proxy_http_version 1.1;

chunked_transfer_encoding off;


client_max_body_size 10m;


proxy_store off;

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_buffering off;

proxy_pass http://127.0.0.1:81$request_uri;

        }

    }


]# cat /etc/nginx/conf.d/xyk.conf 

server {

    listen       80;

    server_name www.shxinsheng.wang;


    charset utf-8;

    #error_log  /var/log/nginx/jenkins.error.log;


    port_in_redirect   on;


keepalive_timeout  15;


location / {


proxy_set_header Connection ‘‘;

proxy_http_version 1.1;

chunked_transfer_encoding off;


client_max_body_size 10m;


proxy_store off;

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_buffering off;

proxy_pass http://127.0.0.1:8082$request_uri;

        }

    }


]# cat /etc/nginx/conf.d/khcm.conf 

server {

    listen       80;

    server_name new.khcm.net;


    charset utf-8;

    #error_log  /var/log/nginx/jenkins.error.log;


    port_in_redirect   on;


keepalive_timeout  15;


location / {


proxy_set_header Connection ‘‘;

proxy_http_version 1.1;

chunked_transfer_encoding off;


client_max_body_size 10m;


proxy_store off;

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_buffering off;

proxy_pass http://127.0.0.1:82$request_uri;

        }

    }


httpd主配置文件:

]# egrep -v ‘^[[:space:]]*#|^$‘ /etc/httpd/conf/httpd.conf 

ServerRoot "/etc/httpd"

Listen 82

Include conf.modules.d/*.conf

User apache

Group apache

ServerAdmin root@localhost

<Directory />

    AllowOverride none

    Require all denied

</Directory>

<Directory "/var/www">

    AllowOverride None

    Require all granted

</Directory>

<Directory "/var/www/html">

    Options Indexes FollowSymLinks

    AllowOverride None

    Require all granted

</Directory>

<IfModule dir_module>

    DirectoryIndex  index.html

</IfModule>

<Files ".ht*">

    Require all denied

</Files>

ErrorLog "logs/error_log"

LogLevel warn

<IfModule log_config_module>

    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>

      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio

    </IfModule>

    CustomLog "logs/access_log" combined

</IfModule>

<IfModule alias_module>

    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

</IfModule>

<Directory "/var/www/cgi-bin">

    AllowOverride None

    Options None

    Require all granted

</Directory>

<IfModule mime_module>

    TypesConfig /etc/mime.types

    AddType application/x-compress .Z

    AddType application/x-gzip .gz .tgz

    AddType text/html .shtml

    AddOutputFilter INCLUDES .shtml

</IfModule>

<IfModule mime_magic_module>

    MIMEMagicFile conf/magic

</IfModule>

EnableSendfile on

IncludeOptional conf.d/*.conf


httpd虚拟主机配置文件:

]# cat /etc/httpd/conf.d/vhost.conf 

DirectoryIndex index.php

Listen 8081

Listen 8082

Listen 8083

Listen 81

<VirtualHost *:8082>

ServerName www.shxinsheng.wang

DocumentRoot /var/www/wwwroot

ProxyRequests off

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/wwwroot/$1

<Directory "/var/www/wwwroot">

Options None

AllowOverride None

Require all granted

</Directory>

</VirtualHost>


<VirtualHost *:8081>

ServerAdmin root

DocumentRoot /usr/share/phpMyAdmin

<Directory "/usr/share/phpMyAdmin">

Options FollowSymLinks

AllowOverride None

Require all granted

</directory>

</VirtualHost>


<VirtualHost *:81>

ServerName www.bbtw.net

DocumentRoot /var/www/banbiantian

ProxyRequests off

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/banbiantian

    <Directory "/var/www/banbiantian">

   Options None

   AllowOverride None

   Require all granted

    </Directory>

</VirtualHost>


<VirtualHost *:82>

ServerName new.khcm.net

DocumentRoot /var/www/khcm

ProxyRequests off

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/khcm

    <Directory "/var/www/khcm">

   Options None

   AllowOverride None

   Require all granted

    </Directory>

</VirtualHost>


启动nginx、httpd、php-fpm即可

nginx反代

标签:nginx反代

原文地址:http://linsj.blog.51cto.com/5440766/1859370

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!