一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

WordPress搬家到Nginx上出现500错误解决办法

时间:2016-06-30 编辑:简简单单 来源:一聚教程网

wordpress从apache上迁移到了nginx,数据库配置无误后仍然打不开,检查http状态,结果返回500,如下图:

之前在apache下.htaccess配置错误的时候会遇到类似的错误,于是首先想到nginx的伪静态配置有问题,nginx下wordpress用的伪静态规则如下:

location / {  
if (-f $request_filename/index.html){  
                rewrite (.*) $1/index.html break;  
        }  
if (-f $request_filename/index.php){  
                rewrite (.*) $1/index.php;  
        }  
if (!-f $request_filename){  
                rewrite (.*) /index.php;  
        } 

禁用伪静态之后仍然打不开,全新安装一个wordpress,使用上面的伪静态规则,可以正常访问,证明伪静态规则无误.

如果首页能打开,而内页打不开,就很有可能是伪静态的问题,可以查看:linux nginx下WordPress伪静态设置解决只能打开首页不能打开内页的问题.

然后突然发现php文件不可执行,怀疑是这个原因造成的,于是将所有php文件加上了可执行属性,仍然不行,看刚才那个全新安装的wordpress,php没有可执行属性一样运行的好好的,看来不是这个原因。虽然这个方法没有解决问题,但也是有收获的,用 chmod 755 -R *.php 并不能按照预想的那样把当前目录及子目录全部php文件加上可执行属性,正确的方法应该是:

find path -type f -exec chmod 755 {} \; 

解释一下 {}是将find的结果一个个的传递给chmod作为参数,分号";"是-exec的必须参数,告诉exec命令结束了,但 ";" 也是shell命令结束的关键字,所以要转义或者引用,即也可以写作:

find path -type f -exec chmod 755 {} ";" 

后来通过搜索发现,缓存插件会有影响,才想起自己安装了object-cache缓存插件,将object-cache.php移走,果然可以正常访问了,可是,没有说在nginx下面就不能用object-cache呀,是不是得重启memcached呀?

原来是还没装memcached !!!!

需要的软件:memcached-1.4.5-1.el5.kb.i386.rpm
             nginx-1.0.15.tar.gz
             agentzh-memc-nginx-module-v0.13rc3-1-gee3fe43.tar.gz
             agentzh-srcache-nginx-module-v0.13rc6-6-g01829d9.tar.gz
             ngx_http_upstream_keepalive-d9ac9ad67f45.tar.gz (这些软件我都会提供给大家)

1.安装memcached


rpm -ivh memcached-1.4.5-1.el5.kb.i386.rpm

我比较懒,不想编译安装memcached,如果有折腾帝想要折腾也可以去下memcached的tar.gz包来编译安装.
启动memcached
memcached -d -m 10 -u root -l 192.168.10.5 -p 11211 -c 256 -P /tmp/memcached.pid

2.编译nginx并安装上第3方模块包

./configure --user=nginx --group=nginx --add-module=../agentzh-memc-nginx-module-ee3fe43 \
--add-module=../agentzh-srcache-nginx-module-01829d9 --add-module=../ngx_http_upstream_keepalive-d9ac9ad67f45  \
--prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf  \
--error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
--pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --with-http_secure_link_module \
--with-http_random_index_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module \
--with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module \
--with-http_stub_status_module --with-http_perl_module --with-http_geoip_module --with-mail \
--with-mail_ssl_module --with-cc-opt=-O3 --with-cpu-opt=pentium

如果编译出错,大家可以去看我这篇文章编译安装nginx并修改版本头信息

make && make install

3.配置nginx

user  nginx;
worker_processes  2;
worker_cpu_affinity 0001 0010;
worker_rlimit_nofile 65535;

error_log   /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

events {
    use epoll;
    worker_connections  65535;
}

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  /var/log/nginx/access.log  main;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 4k;
    large_client_header_buffers 4 32k;
    client_body_in_file_only clean;
    client_max_body_size 8m;

    sendfile        on;
    tcp_nopush      on;

    keepalive_timeout  60;
    tcp_nodelay on;
    server_tokens   off;

    fastcgi_connect_timeout 300s;
    fastcgi_send_timeout 300s;
    fastcgi_read_timeout 300s;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 8 128k;#8 128
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_intercept_errors on;
 
    #hiden php version
    fastcgi_hide_header X-Powered-By;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    #gzip_disable "MSIE [1-5]\.";
    gzip_comp_level 4;
    gzip_types text/plain application/x-javascript text/css application/xml image/gif image/jpg image/jpeg image/png;
    #gzip_vary on;
    proxy_hide_header Vary;

    #Memcache Service upstream
    upstream memcache{
        server 192.168.10.5:11211;
        keepalive 512 single;
        }

    server {
        listen       80;
        server_name blog.slogra.com;
    #memc-nginx-module
    location /memc {
        internal;
        memc_connect_timeout 100ms;
        memc_send_timeout 100ms;
        memc_read_timeout 100ms;
        set $memc_key $query_string;
        set $memc_exptime 300;
        memc_pass memcache;
        }
    location / {
    root /var/www/vhosts/blog.slogra.com;
    index index.php index.html index.htm;
    #srcache-nginx-module
    set $key $uri$args;
    srcache_fetch GET /memc $key;
    srcache_store PUT /memc $key;
   
        location ~ .*\.(php|php5)?$ {
            fastcgi_pass   unix:/tmp/php-cgi.sock;
            fastcgi_index  index.php;
            include  fastcgi.conf;
        }
        }
    }
}

ps:
memc-nginx是一个标准的upstream模块,因此首先需要定义memcache的upstream.
这里我在本机上启动了一个memcache服务,端口为默认的11211,keepalive指令是http-upsteram-keepalive-module提供的功能, 这里我们最大保持512个不立即关闭的连接用于提升性能.

下面是为memc-nginx-module配置location,我们配置为/memc,所有请求都通过请求这个location来操作 memcache.
memc-nginx-module存取memcache是基于http method语义的,使用http的GET方法表示get、PUT方法表示set、DELETE方法表示delete.
这里我们将/memc设为internal表示只接受内部访问,不接收外部http请求,这是为了安全考虑,当然如果需要通过http协议开放外部访问,可以去掉internal然后使用deny和allow指 令控制权限.比较重要的是$memc_key这个变量,它表示以什么作为key,这里我们直接使用Nginx内置的$query_string来作为 key,$memc_exptime表示缓存失效时间,以秒记.这里统一设为300(5分钟),在实际应用中可以根据具体情况为不同的内容设置不同的过期时间.

最后我们为“/”这个location配置了缓存,这表示所有的请求都会结果被缓存,当然这里只是示例需要,实际中一般不会这么配,而是为特定需要缓存的location配置缓存. 比如只缓存图片,js,css等资源文件.

srcache_fetch表示注册一个输入拦截处理器到location,这个配置将在location进入时被执行;
而 srcache_store表示注册一个输出拦截器到location,当location执行完成并输出时会被执行.
注意srcache模块实际可以与任何缓存模块进行配合使用,而不必一定是memc.这里我们以$uri$args作为缓存的key.

经过上述配置后,相当于对Nginx增加了如下逻辑:当所请求的uri以“.php”结尾时,首先到memcache中查询有没有 以$uri$args为key的数据,如果有则直接返回;否则,执行location的逻辑,如果返回的http状态码为

装完memcached,终于可以访问了.

热门栏目