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即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章