我在专用的服务器上运行Debian Squeeze,将其用作Web和数据库服务器(nginx,apache和mysql)。
我跟着几个关于如何设置nginx作为apache的反向代理的教程 – 大部分工作正常,我不得不说 – 哇,nginx是非常快的。
但是,他们也是我在“testing”期间遇到的一些问题。 在这里,我希望serverfault能够帮助我的人。 🙂
所以让我先给你看我完整的configuration和解释场景。
脚本
要pipe理服务器上的客户端,我正在使用ISPConfig控制面板,configuration为处理Apache(您可以selectNginx或Apache之间)。
因为我不希望客户端必须configuration特殊的重写规则等我试图调整nginx非常透明,这意味着它确实只提供静态文件和所有其他请求传递给Apache,所以重写规则等仍然工作。
组态
我目前拥有的是:
mod_rpaf启用转发真正的IP到Apache
nginx使用以下configuration安装:
/etc/nginx/nginx.conf
user www-data; worker_processes 4; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 2048; } http { include /etc/nginx/mime.types; access_log /var/log/nginx/access.log; sendfile on; tcp_nopush on; keepalive_timeout 4; tcp_nodelay on; # Hide version information server_tokens off; # Include configurations include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }
/etc/nginx/conf.d/gzip.conf
gzip on; # Compression level gzip_comp_level 6; # HTTP version gzip_http_version 1.0; # File min lenght to compress gzip_min_length 0; # Compress all proxied files gzip_proxied any; # Mimes to compress gzip_types text/plain text/css application/x-javascript text/xml \ application/xml application/xml+rss text/javascript; # Disable for IE 6 and below gzip_disable "MSIE [1-6]\."; gzip_vary on;
/etc/nginx/conf.d/cache.conf
# Set locations and sizes proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=nginx_cache:10m max_size=500m; proxy_temp_path /tmp/nginx; # Putting the host name in the cache key allows different virtual hosts to share the same cache zone proxy_cache_key "$scheme://$host$request_uri"; # Cache different return codes for different lengths of time proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m;
/etc/nginx/conf.d/proxy.conf
proxy_redirect off; # Set proxy headers proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # Client configuration client_max_body_size 10m; client_body_buffer_size 128k; client_header_buffer_size 64k; # Connection and buffer proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffer_size 16k; proxy_buffers 32 16k; proxy_busy_buffers_size 64k;
在/ etc / nginx的/网站可用/默认
server { # Listen on Port 80 listen 80 default; # Resolve server_name with DNS server_name _; server_name_in_redirect off; resolver 213.133.100.100; # Strip www from host if ($host ~* ^www\.(.*)) { set $cleanhost $1; } if ($cleanhost = "") { set $cleanhost $host; } access_log /var/log/ispconfig/httpd/$cleanhost/access.log; # Serve static files through nginx #location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|e$ location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|ex$ root /var/www/$cleanhost/web; access_log off; expires max; error_page 404 /; } # Apache backend for dynamic files location / { root /var/www/$cleanhost/web; index index.html index.htm index.php; access_log off; proxy_cache nginx_cache; proxy_pass http://$cleanhost:82; proxy_pass_header Set-Cookie; } }
做什么的?
正如你所看到的,我试图通过nginx来提供静态文件并压缩它们。 所有其他请求都传递给apache,结果从nginxcaching(在tmpfs中)。
有了这个默认网站,我不必为服务器上的每个域创build一个configuration。
问题
好的,既然你已经看过configuration,那么让我们来看看这些configuration所带来的问题:
如果一个网站使用mod_rewrite来重写像index.php?page = home到* / page / home.html的URL,nginx认为这是一个静态文件(因为.html结尾) – 我得到一个错误“not found ”。
在WordPress的博客,你不能插入媒体到post。 你可以上传它们,但只要你按下“插入后”button,你会得到一个403 – 禁止的错误。
每个网站都有自己的错误文档文件夹/var/www/domain.tld/web/errors/[404.html|500.html等]。 我如何告诉nginx / apache来提供这些而不是nginx / apache的默认错误页面?
什么会帮助我?
如果有人可以遍历configuration并查找部件,这可能会导致命名的错误/问题。
此外,一般来说,与性能/安全等有关的一般提示和tipps赞赏:)
非常感谢您的时间和帮助!
问候,MaddinXx
有两件事:你不应该使用debian版本。 我正在吃cookies,这花了我半天的时间才弄清楚。 使用由nginx人提供的最新debian软件包。 他们提供回购,所以相当方便。
对于redirect:我看你正在使用proxy_redirect off; 。 尝试类似
proxy_redirect http://$host/index.php?page=home http://$host/page/home.html;