安哥网络 发表于 2015-4-13 13:31:23

Nginx+Apache配置详解

Nginx+Apache配置详解
我们依然尽可能采用yum来安装我们需要的软件,由系统官方维护的软件,其安全性和稳定性都值得信赖,并且容易管理,升级方便,但是在CentOS和RHEL的官方yum源中暂时没有Nginx等软件包,所以我们需要使用EPEL的yum源。EPEL是什么,EPEL的yum源怎么添加,点击这里 http://www.linuxidc.com/Linux/2012-10/71850.htm 查看。
本文将要介绍的Nginx+Apache结构,其实就是Nginx做前端,Apache做后端,充分发挥他们各自的优势之处。Nginx对于高并发性能出众,Proxy功能强效率高,占用系统资源少,而Apache在高并发时对队列的处理比FastCGI(Nginx需要通过fastcgi等方式运行php)更好,并且在处理动态php页面时,mod_php也比php-cgi更稳定更高效。
也就是说,我们的目的是,由Nginx来接收客户端的请求,如果是动态页面请求,就交给Apache处理,然后经由Nginx再返回给客户端,其余的请求,则由Nginx自己处理,然后把结果返回给客户端,。当然了,你完全可以让Nginx只做Proxy功能,所有的请求都交给Apache,Tomcat等处理,本文使用前者。
但是,在本文中,我们实现的是在一台服务器里一个Nginx加一个Apache的简单结构,在实际应用中,可能前端是由一台或多台Nginx组成的代理服务器,后端则是多台Apache或IT论坛Web服务器,再加上多种第三方软件而组成的集群。
前提约定:假设我们系统默认主站点名是www.linuxidc.com,网站根目录是/var/www/linuxidc
第一步,安装并配置Nginx,安装完之后,配置文件都在/etc/nginx目录下,主设置文件/etc/nginx/nginx.conf:
test ~]# yum -y install nginx
test ~]# vi /etc/nginx/nginx.conf
user            nginx;
worker_processes2;

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

events {
    use epoll;
    worker_connections2048;
}

http {
    include      /etc/nginx/mime.types;
    include      /etc/nginx/proxy.conf;
    include      /etc/nginx/gzip.conf;
    default_typeapplication/octet-stream;
    index      index.html index.htm index.php;

    log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
            '$status $body_bytes_sent "$http_referer" '
            '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile          on;
    tcp_nopush      on;
    server_tokens      off;
    keepalive_timeout60;
    server_names_hash_bucket_size 128;

    server {
      listen      80;
      server_namewww.linuxidc.com;
      root      /var/www/linuxidc;
      location ~* .*\.(gif|jpg|jpeg|png|bmp|ico|css|js|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ {
            expires 2d;
      }
      # 如果你需要客户端缓存的内容以及媒体,图片等文件固定放置一些目录下的话,就把上面那个
      # location注释掉,用下面这种方式
      # location ~ ^/(images|javascript|js|css|flash|media|static)/{
      #expires 2d;
      #}
         
      location ~ .*\.(php?|cgi|pl|py)$ {
            proxy_pass http://127.0.0.1:8888;
      }
      location ~ /\.ht {
            denyall;
      }
      location ~ /.+\.(inc|conf|cnf) {
            denyall;
      }
      access_log/var/log/nginx/linuxidc-access.log main;
      #access_log off
    }
    # Load config files from the /etc/nginx/conf.d directory
    include /etc/nginx/conf.d/*.conf;
}
test ~]# vi /etc/nginx/proxy.conf
proxy_redirect          off;
proxy_hide_header      Vary;
proxy_set_header      Accept-Encoding '';
proxy_set_header      Host            $host;
proxy_set_header      X-Real-IP      $remote_addr;
proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size      4k;
proxy_buffers          32 4k;
proxy_busy_buffers_size 64k;

test ~]# vi /etc/nginx/gzip.conf
gzip on;
gzip_http_version 1.0;
gzip_disable      "MSIE \.";
gzip_disable      "Mozilla/4";
gzip_comp_level3;
gzip_proxied      any;
gzip_vary      on;
gzip_buffers      4 16k;
gzip_min_length1100;
gzip_types      text/plain text/xml text/css application/xml application/xhtml+xml application/rss+xml application/atom_xml application/javascript application/x-javascript;

为了让nginx.conf简洁,我把一些相关的共通设定放到同一个专门的文件里,然后在主配置文件nginx.conf里加载。如果你使用了VirtualHost运营多个站点,你可以根据不同站点的需求而配置不同的设定文件,然后分别在各自的server域里加载。
第二步,安装并配置Apache,配置文件在/etc/httpd/下,我们修改主配置文件/etc/httpd/conf/httpd.conf,配置文件太长,我只写我需要改的地方:
test ~]# yum -y install httpd
test ~]# vi /etc/httpd/conf/httpd.conf
Listen 80
↓改成
Listen 127.0.0.1:8888

DocumentRoot "var/www/html"
↓改成
DocumentRoot "/var/www/linuxidc"

<Directory "/var/www/html">
↓改成
<Directory "/var/www/linuxidc">
    Options Indexes FollowSymLinks
    ↓改成如下,允许CGI,SSI
    Options Includes ExecCGI FollowSymLinks

    AllowOverride None
    ↓改成如下,支持.htaccess
    AllowOverride All

    Order allow,deny
    Allow from all
</Directory>

ServerSignature On
↓改成如下,在现实错误页面的时候不会显示服务器和apache的版本
ServerSignature Off

#AddHandler cgi-script .cgi
↓改成如下,cgi脚本使用.cgi,.pl,.py
AddHandler cgi-script .cgi .pl .py

Nginx+Apache配置详解
摘自:http://www.linuxidc.com/Linux/2012-10/71851.htm
页: [1]
查看完整版本: Nginx+Apache配置详解