nginx反向代理配置實例(前nginx+後apache)javascript
我就拿我如今這個站的環境給你們看看..php
若是是一臺普通vps或者是獨立服務器 ,,,首先咱們要乾的就是裝環境和配置防火牆了..css
首先咱們配置下防火牆吧,html
[root@51buyhost.com ~]# vim /etc/sysconfig/iptablesjava
:wq! 保存退出node
你們確定會問 88 89 端口是幹嗎的,,不用問了,等會看就知道了...mysql
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPTlinux
-A INPUT -m state --state NEW -m tcp -p tcp --dport 88 -j ACCEPTnginx
-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPTsql
-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>