nginx 反向代理

简介: nginx反向代理配置实例(前nginx+后apache)   我就拿我现在这个站的环境给大家看看..    如果是一台普通vps或者是独立服务器 ,,,首先我们要干的就是装环境和配置防火墙了..    首先我们配置下防火墙吧,  [root@51buyhost.

nginx反向代理配置实例(前nginx+后apache)

  我就拿我现在这个站的环境给大家看看..

   如果是一台普通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=$2:$3&%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>

目录
相关文章
|
3月前
|
应用服务中间件 nginx
百度搜索:蓝易云【如何用NGINX实现UDP四层反向代理?】
请注意,这个回答是基于NGINX目前的特性和功能,如果有新的版本或更新的特性,建议查阅NGINX官方文档或其他权威资源,以确保你得到最准确的配置信息。
31 0
|
3月前
|
JSON 应用服务中间件 API
利用Grafana的API Key+Nginx反向代理实现Grafana免登录访问
利用Grafana的API Key+Nginx反向代理实现Grafana免登录访问
86 1
|
5月前
|
负载均衡 网络协议 算法
Nginx系列教程(13) - TCP反向代理实现
Nginx系列教程(13) - TCP反向代理实现
399 0
|
3月前
|
负载均衡 应用服务中间件 nginx
nginx-tomcat反向代理以及负载均衡测试
nginx-tomcat反向代理以及负载均衡测试
|
5月前
|
应用服务中间件 nginx Docker
nginx反向代理踩坑(容器方式)
nginx反向代理踩坑(容器方式)
179 0
|
5月前
|
负载均衡 Unix 应用服务中间件
深入解析HTTP反向代理:探索NGINX的神奇之处
深入解析HTTP反向代理:探索NGINX的神奇之处
|
1月前
|
负载均衡 应用服务中间件 Linux
|
2月前
|
tengine Rust 负载均衡
反向代理学习笔记(一) Nginx与反向代理绪论
反向代理学习笔记(一) Nginx与反向代理绪论
|
2月前
|
负载均衡 Java 应用服务中间件
|
2月前
|
消息中间件 关系型数据库 MySQL
使用Nginx的stream模块实现MySQL反向代理与RabbitMQ负载均衡
使用Nginx的stream模块实现MySQL反向代理与RabbitMQ负载均衡
59 0