最近一个客户将Joomla4网站从原先的Apache服务器改为Nginx服务器,整个过程一切顺利,但还原网站后发现只能打开首页,其他页面都是404。这个问题需要修改nginx的配置文件来解决。

伪静态

在Apache中使用.htaccess来完成伪静态路由的转发,但Nginx不是这个机制,需要修改配置文件来完成。

默认的nginx配置文件为 nginx.conf。如果使用的是面板,可能每一个网站都有一个配置文件,修改自己网站对应的配置文件即可。

编辑Nginx的配置文件,在配置文件中加入下面的一行:

location / {
                try_files $uri $uri/ /index.php?$args;
        }

加完成功后,保存重启nginx。再次刷新页面就正常了。

完整的Nginx.conf代码

上面的代码是实现路由转换的关键代码,nginx.conf内容还可以加入一些其他的设置,完整的内容如下:

server {
        listen 80;
        server_name YOUR_DOMAIN;(你要绑定的域名,多个域名用空格分开)
        server_name_in_redirect off;

        access_log /var/log/nginx/localhost.access_log;(设置日志文件)
        error_log /var/log/nginx/localhost.error_log info;(设置日志文件)

        root PATH_ON_SERVER;(网站存放目录)
        index index.php index.html index.htm default.html default.htm;
        # Support Clean (aka Search Engine Friendly) URLs
        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        # deny running scripts inside writable directories
        location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
                return 403;
                error_page 403 /403_error.html;
        }

        location ~ \.php$ {
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include /etc/nginx/fastcgi.conf; 
        }

        # caching of files 
        location ~* \.(ico|pdf|flv)$ {
                expires 1y;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
                expires 14d;
        }

}

 注意将上面的路径换成你自己网站的实际路径。

 

作者: 樱木花道

Joomla程序员,从J1.5到J4.x始终都在做Joomla相关开发定制工作,有超过10年行业经验,国内Joomla扩展开发商ZMAX团队的核心成员

作者网站:ZMAX程序人

评论 (0)

  • 最新在前
  • 最佳在前