nginx與apache一塊兒私奔-反向代理

nginx與apache一塊兒作反向代理,各自的優勢就不細說了,反正一個處理併發及靜態的牛差差,一個處理動態的牛差差。php

想看apache如何運行安裝,能夠查看個人另外一篇centOS7 LAMP安裝及注意要點css


一、apache相關配置文件更改端口號html

主配置文件:node

vi /etc/httpd/conf/httpd.conf
Listen 81

虛擬主機配置文件:nginx

vi /etc/httpd/conf.d/lock.com.conf

<VirtualHost 192.168.136.128:81>
    DirectoryIndex index.php
    ServerAdmin 2871903572@qq.com
    DocumentRoot /www/lockcom
    ServerName lock.com
    ServerAlias lock.com

        <Directory /www/lockcom>
                Options Indexes FollowSymLinks
                AllowOverride None
                Require all granted
        </Directory>

</VirtualHost>


打開瀏覽器shell

http://lock.com:81apache

應該能夠訪問。瀏覽器


二、nginx安裝併發

用yum來安裝吧app

yum nginx install
/bin/systemctl start  nginx.service
systemctl status nginx.service

打開瀏覽器

http://lock.com

應該是nginx歡迎頁面


三、更改nginx相關配置文件

nginx.conf主配置文件:

主要是proxy_pass作反向代理

若是你在apache虛擬主機用了固定IP地址話,proxy_pass應該是同樣的IP地址,若是沒有設置,正經常使用127.0.0.1就能夠。

同時創建nginx的虛擬主機配置目錄,並在主配置文件下包含:include /etc/nginx/vhost/*.conf;

mkdir /etc/nginx/vhost
touch /etc/nginx/vhost/lock.com.conf

下面nginx主配置文件:

nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

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;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        #listen       80 default_server;
        #listen       [::]:80 default_server;
        listen       80;
        server_name  _;
        #root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        # include /etc/nginx/default.d/*.conf;

       
        location / {
                try_files $uri @apache;
                }


        location @apache {
                internal;
                proxy_pass http://192.168.136.128:81;
                }

        location ~ .*\.(php|php5)?$  {
                proxy_pass http://192.168.136.128:81;
                }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
                expires 30d;
                }

        location ~ .*\.(js|css)?$ {
                expires 7d;
                }


        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    include /etc/nginx/vhost/*.conf;
}

虛擬主機下的配置vhost/lock.com.conf

vi /etc/nginx/vhost/lock.com.conf
server {
listen 80;
server_name lock.com;
access_log /www/wwwlogs/lock.com_nginx.log combined;
index index.html index.htm index.jsp index.php;
root /www/lockcom;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
        }
if ( $http_user_agent ~ ApacheBench|WebBench|Jmeter|must-revalidate|Havij ){
        return 503;
        }


location ~ .*\.(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv)$ {
        valid_referers none blocked nahehuo.com *.nahehuo.com;
        if ($invalid_referer) {
                #rewrite ^/ http://www.lock.com/403.html;
                return 403;
                }
        }
limit_rate 500k;
location / {
        try_files $uri @apache;
        }

location @apache {
        internal;
        proxy_pass http://192.168.136.128:81;
        }

location ~ .*\.(php|php5)?$ {
        proxy_pass http://192.168.136.128:81;
        }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
        expires 30d;
        }

location ~ .*\.(js|css)?$ {
        expires 7d;
}

}
相關文章
相關標籤/搜索