最近一個客戶將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) URLslocation / {try_files $uri $uri/ /index.php?$args;}# deny running scripts inside writable directorieslocation ~* /(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;}}
注意將上面的路徑換成你自己網站的實際路徑。