nginx反向代理配置实例(前nginx+后apache)
Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过三年了。Igor 将源代码以类BSD许可证的形式发布。Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,大部分门户网站都把它作为首选WEB前端。
nginx与apache相比,处理静态页面很强大,对于动态页面,它不及apache
用nginx 前台处理静态http请求,用apache后台处理php动态页面。
主机用相同的根目录,但访问的端口不一样。nginx 用80,apache用88。当访问http://10.0.0.99的时候,你访问的是(nginx),它会在根目录下面查找静态文件(css ,gif,html ·······),如果找到它会把信息直接返回给浏览器,如果找不到(像.php的文件),它会通过( proxy_pass: http://10.0.0.99:88/;)转向apache相对应的路径请求。
一、安装nginx
nginx安装需要 pcre 源码的支持
建立用户及组
[root@localhost /]# /usr/sbin/groupadd www
[root@localhost /]# /usr/sbin/useradd -g www www
[root@localhost ~]# cd /home/admin/
[root@localhost admin]# tar zvxf nginx-1.5.6.tar.gz
[root@localhost admin]# cd nginx-1.5.6
[root@localhost nginx-1.5.6]# mkdir /usr/local/nginx
[root@localhost nginx-1.5.6]# ./configure --user=www --group=www \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-openssl=/usr \
--with-pcre=/home/admin/pcre-8.33 \#pcre源码包路径
--with-cc-opt='-O2'\
--with-cpu-opt=你的cpu型号
注意:--with-pcre=/home/admin/pcre-8.33指向的是源码包解压的路径,而不是安装的路径,否则会报错
[root@localhost nginx-1.5.6]# make && make install
#注意上文中的--with-cc-opt='-O2' --with-cpu-opt=opteron 这是编译器优化,目前最常用的是-02 而不是3.后面对应CPU的型号
#启动nginx
[root@localhost nginx-1.5.6]# /usr/local/nginx/sbin/nginx
Nginx开启命令:/usr/local/nginx/sbin/nginx
Nginx停止命令: /usr/local/nginx/sbin/nginx -s stop
Nginx的热启动:#usr/local/nginx/sbin/nginx -s reload
#修改配置文件
[root@localhost nginx-1.5.6]# cd /usr/local/nginx/conf/ [root@localhost conf]# cp nginx.conf nginx.conf.back #备份
nginx 处理下图片,html等静态的东西.其它动态由apache处理.因此apache也需要做一些参数调整.
设置图片等过期时间.缓解请求.
[root@localhost conf]# vi nginx.conf
#user nobody;
worker_processes 8;
error_log /usr/local/nginx/logs/error.log; #配置错误日志目录
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /usr/local/nginx/logs/nginx.pid; #配置进程pid
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on; #发送文件功能打开
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 88; #nginx 默认端口是80
server_name 10.0.0.99; #服务器域名
#charset koi8-r;
#access_log logs/host.access.log main;
#access_log logs/host.access.log main;
location / {
proxy_pass http://10.0.0.99:80/; #nginx代理的服务器
proxy_redirect default;
root /home/wwwroot; #项目跟目录
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.php$ {
proxy_pass http://10.0.0.99:80; #所有的php文件转向apache
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
---------------
修改apache配置文件
Listen 80 #修改成你的端口
<VirtualHost *:80> #依据你的环境修改
ServerName 10.0.0.99
DocumentRoot "/home/wwwroot"
</VirtualHost>
来源:http://www.cnblogs.com/iyoule/p/3389794.html