nginx反向代理配置實例(前nginx+后apache)靜,動態核心分離
nginx反向代理配置實例 此帖子還是借鑒任俊兄弟給的配置文件參考修整的....
我就拿我現在這個站的環境給大家看看..
如果是一台普通vps或者是獨立伺服器 ,,,首先我們要乾的就是裝環境和配置防火牆了..
首先我們配置下防火牆吧,
[
[email protected] ~]# 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
可能大家會覺得我啰嗦.....錯...這些 我主要是對新手來講的,,,, 如果你是老手你跳到最下面看的我配置文件就行...
[
[email protected] ~]# vim /etc/sysconfig/selinux 打開selinux 配置文件
在裡面把所有的都註釋掉新增一個
SELINUX=disabled
:wq! 保存退出
重啟 伺服器[
[email protected] ~]# reboot
等伺服器重啟完畢之後 我們就開始安裝環境了.. 注意,,我給大家介紹的全部是yum 源安裝 .喜歡編譯的安裝的自己 在編譯安裝之前需安裝編譯需要的依賴包以及 gcc 等等那些工具...在此我提醒大家.很多人 的伺服器環境是最小化安裝版, 最小化安裝版是不適合 編譯安裝環境的..如果是的話請手動用 yum 安裝所需要的編譯環境.. 不說了 咱們開始
首先安裝nginx吧 這種方法是教大家安裝nginx 最新版本的..
[
[email protected] ~]# vim /etc/yum.repos.d/CentOS-Base.repo
在最後一行加上如下內容
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
:wq! 保存退出
下面開始安裝nginx了
[
[email protected] ~]# yum install nginx
提示按 y
安裝 mysql
[
[email protected] ~]# yum install mysql-server
提示按 y
安裝php
[
[email protected] ~]# yum install php -y
提示按 y
安裝php的擴展插件
[
[email protected] ~]# 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 這個配置文件備份下吧.
[
[email protected] ~]#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配置文件比較多 我只告訴大家我修改了哪些地方而已
# vim /etc/httpd/conf/httpd.conf
在大概136行的樣子增加以下內容
把默認的 80 埠註釋
Listen 127.0.0.1:88
還有在最下面增加以下內容
Include /etc/httpd/conf/51buyhost.conf
我給大家最好都是默認的配置吧,因為 個人有個人的配置訪問 ,,我測試的時候 只在apache配置文件里加了以上內容
接下來就設置51buyhost.conf 的內容
上面帶了dz 偽靜態的規則
DocumentRoot "/data/51buyhost"
ServerName www.51buyhost.com
ServerAlias 51buyhost.com
allow from all
Options +Indexes
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/article-(+)-(+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/forum-(\w+)-(+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/thread-(+)-(+)-(+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page\%3D$4&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/group-(+)-(+)\.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-(+)-(+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/(+*)-(+)\.html$ $1/plugin.php?id=$2:$3&%1
RewriteCond %{http_host} ^51buyhost.com
RewriteRule ^(.*)$ http://www.51buyhost.com
CacheEnable mem /
MCacheMaxObjectCount 20000
MCacheMaxObjectSize 1048576
MCacheMaxStreamingBuffer 65536
MCacheMinObjectSize 10
MCacheRemovalAlgorithm GDSF
MCacheSize 4096
CacheMaxExpire 864000
CacheDefaultExpire 86400
CacheDisable /php
#
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 "
#
完畢之後重啟apache 和nginx ...
搞定了 就這樣簡單...
如果有什麼疑問直接 下面跟帖 回復我...
《解決方案》
帖子不錯,感覺有改進的地方.yum -y install 更好。
《解決方案》
nginx我一般是自己編譯安裝的,可以定製一些特性,比如ngx_status
《解決方案》
好貼馬克一下
《解決方案》
ding_cw 發表於 2013-06-25 17:15 static/image/common/back.gif
好貼馬克一下
感謝支持哦
《解決方案》
看完不頂什麼心態?不夠覺悟嗎?火前留名不知嗎?強勢插了不知嗎?