免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10753 | 回复: 5
打印 上一主题 下一主题

[其他] nginx反向代理配置实例(前nginx+后apache)静,动态核心分离 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-05-29 11:34 |只看该作者 |倒序浏览
nginx反向代理配置实例  此帖子还是借鉴任俊兄弟给的配置文件参考修整的....

  我就拿我现在这个站的环境给大家看看..
  如果是一台普通vps或者是独立服务器 ,,,首先我们要干的就是装环境和配置防火墙了..
  首先我们配置下防火墙吧,
[root@51buyhost.com ~]# vim /etc/sysconfig/iptables


:wq!  保存退出
大家肯定会问 88 89 端口是干嘛的,,不用问了,等会看就知道了...
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 88 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

还有一件事需要做的就是selinux   
可能大家会觉得我啰嗦.....错...这些 我主要是对新手来讲的,,,, 如果你是老手你跳到最下面看的我配置文件就行...
[root@51buyhost.com ~]# vim /etc/sysconfig/selinux  打开selinux 配置文件
在里面把所有的都注释掉新增一个
SELINUX=disabled
:wq! 保存退出
重启 服务器[root@51buyhost.com ~]# reboot

等服务器重启完毕之后 我们就开始安装环境了..  注意,,我给大家介绍的全部是yum 源安装 .喜欢编译的安装的自己 在编译安装之前需安装编译需要的依赖包以及 gcc  等等那些工具...在此我提醒大家.很多人 的服务器环境是最小化安装版, 最小化安装版是不适合 编译安装环境的..如果是的话请手动用 yum 安装所需要的编译环境.. 不说了 咱们开始
  首先安装nginx吧  这种方法是教大家安装nginx 最新版本的..
[root@51buyhost.com ~]# vim /etc/yum.repos.d/CentOS-Base.repo
在最后一行加上如下内容
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1


:wq! 保存退出
下面开始安装nginx了
[root@51buyhost.com ~]# yum install nginx
提示按 y
安装 mysql
[root@51buyhost.com ~]# yum install  mysql-server
提示按 y
安装php
[root@51buyhost.com ~]# yum install php -y
提示按 y
安装php的扩展插件
[root@51buyhost.com ~]# yum install php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel
好了.以上就是 lnmp 环境的完整 安装了.
接下来我们需要把 /etc/nginx/ 目录下面的nginx.conf 这个里面的内容全部修改
最好是先把默认的nginx.conf 这个配置文件备份下吧.
[root@51buyhost.com ~]#cd /etc/nginx
备份重新命名为 nginx.confbak
接下来新建立一个nginx.conf 配置文件
输入一下内容:
user  nginx nginx;
worker_processes 1;

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


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    server_tokens off;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;
    sendfile on;
    tcp_nopush     on;
    keepalive_timeout 60;
    tcp_nodelay on;
   fastcgi_connect_timeout 300;
   fastcgi_send_timeout 300;
   fastcgi_read_timeout 300;
   fastcgi_buffer_size 64k;
   fastcgi_buffers 4 64k;
   fastcgi_busy_buffers_size 128k;
   fastcgi_temp_file_write_size 256k;
   gzip on;
   gzip_min_length  1k;
   gzip_buffers     4 16k;
   gzip_http_version 1.0;
   gzip_comp_level 2;
   gzip_types       text/plain application/x-javascript text/css application/xml;
   gzip_vary on;

    proxy_set_header  Host $host;
    proxy_set_header  X-Real-IP $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

server
        {
                listen       80;
                server_name www.51buyhost.com 51buyhost.com;
               index index.html index.htm index.php;

root  /data/51buyhost;

                location / {
                        try_files $uri @apache;
                        }

                location @apache {
                        internal;
                        proxy_pass http://127.0.0.1:88;
                        #include proxy.conf;
                        }

                location ~ .*\.(php|php5)?$
                        {
                                proxy_pass http://127.0.0.1:88;
                               # include proxy.conf;
                        }

                location /status {
                        stub_status on;
                        access_log   off;
                }

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

                location ~ .*\.(js|css)?$
                        {
                        access_log off;
                         expires      12h;
                        }

                access_log /data/log/51buyhost/access.log;
        }
include /etc/nginx/conf.d/*.conf;


以上是我服务器的主配置文件了..
只真对www.51buyhost.com 这个站的... 虚拟主机配置文件我就不亮出来了.
以上就是nginx 的整个配置文件了.还有一个虚拟主机在里面
下面我亮出我的apache配置文件  
apache配置文件比较多  我只告诉大家我修改了哪些地方而已
[root@51buyhost ~]# vim /etc/httpd/conf/httpd.conf
在大概136行的样子增加以下内容
把默认的 80 端口注释
Listen 127.0.0.1:88
还有在最下面增加以下内容
Include /etc/httpd/conf/51buyhost.conf

我给大家最好都是默认的配置吧,因为 个人有个人的配置访问 ,,我测试的时候 只在apache配置文件里加了以上内容
接下来就设置51buyhost.conf 的内容

上面带了dz 伪静态的规则
<VirtualHost *:88>
DocumentRoot "/data/51buyhost"
ServerName www.51buyhost.com
ServerAlias 51buyhost.com
<Directory "/data/51buyhost">
allow from all
Options +Indexes
</Directory>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page\%3D$4&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$23&%1
RewriteCond %{http_host} ^51buyhost.com [NC]
RewriteRule ^(.*)$ http://www.51buyhost.com [L,R=301]
</IfModule>
<IfModule mod_mem_cache.c>
CacheEnable mem /
MCacheMaxObjectCount 20000
MCacheMaxObjectSize 1048576
MCacheMaxStreamingBuffer 65536
MCacheMinObjectSize 10
MCacheRemovalAlgorithm GDSF
MCacheSize 4096
CacheMaxExpire 864000
CacheDefaultExpire 86400
CacheDisable /php
</IfModule>
#</Directory>
ExpiresActive on
ExpiresBytype text/css "access plus 3 days
ExpiresByType application/x-javascript "access plus 3 days "
ExpiresByType image/jpeg "access plus 3 days "
Expiresbytype image/gif "access plus 3 days "
Expiresbytype image/png "access plus 3 days "
#</Directory>
</VirtualHost>

完毕之后重启apache 和nginx  ...
搞定了 就这样简单...
如果有什么疑问直接 下面跟帖 回复我...

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每月发帖之星
日期:2015-09-11 19:30:52IT运维版块每周发帖之星
日期:2015-09-11 19:20:31IT运维版块每日发帖之星
日期:2015-08-26 06:20:00每日论坛发贴之星
日期:2015-08-20 06:20:00IT运维版块每日发帖之星
日期:2015-08-20 06:20:002015年辞旧岁徽章
日期:2015-03-03 16:54:15金牛座
日期:2014-05-04 16:58:09双子座
日期:2013-12-17 16:44:37辰龙
日期:2013-11-22 15:20:59狮子座
日期:2013-11-18 22:55:08射手座
日期:2013-11-12 10:54:26
2 [报告]
发表于 2013-05-30 14:42 |只看该作者
帖子不错,感觉有改进的地方.yum -y install 更好。

论坛徽章:
381
CU十二周年纪念徽章
日期:2014-01-04 22:46:58CU大牛徽章
日期:2013-03-13 15:32:35CU大牛徽章
日期:2013-03-13 15:38:15CU大牛徽章
日期:2013-03-13 15:38:52CU大牛徽章
日期:2013-03-14 14:08:55CU大牛徽章
日期:2013-04-17 11:17:19CU大牛徽章
日期:2013-04-17 11:17:32CU大牛徽章
日期:2013-04-17 11:17:37CU大牛徽章
日期:2013-04-17 11:17:42CU大牛徽章
日期:2013-04-17 11:17:47CU大牛徽章
日期:2013-04-17 11:17:52CU大牛徽章
日期:2013-04-17 11:17:56
3 [报告]
发表于 2013-05-31 18:57 |只看该作者
nginx我一般是自己编译安装的,可以定制一些特性,比如ngx_status

论坛徽章:
0
4 [报告]
发表于 2013-06-25 17:15 |只看该作者
好贴马克一下

论坛徽章:
0
5 [报告]
发表于 2013-06-26 11:33 |只看该作者
ding_cw 发表于 2013-06-25 17:15
好贴马克一下


感谢支持哦

论坛徽章:
0
6 [报告]
发表于 2013-06-26 13:04 |只看该作者
看完不顶什么心态?不够觉悟吗?火前留名不知吗?强势插了不知吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP