1. 程式人生 > >linux centos7 下Nginx伺服器實現URL重寫去掉index.php方法,跟Apache還是不一樣的。

linux centos7 下Nginx伺服器實現URL重寫去掉index.php方法,跟Apache還是不一樣的。

想要的URL :http://localhost/Admin/ 而自己的是http://localhost/index.php/Admin/

方法:

在nginx配置檔案nginx.conf中新增:

location / {

  if ( !e $request_filename ) {

    rewrite ^/(.*)$ /index.php/$1 last;

  }

}

切記:不可以出現兩個location / {}、否則nginx伺服器將啟動不了;

否則報錯如下:

systemctl status nginx.service -l

● nginx.service - SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2018-06-21 10:37:14 CST; 4s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 28469 ExecStop=/etc/rc.d/init.d/nginx stop (code=exited, status=0/SUCCESS)
  Process: 19590 ExecReload=/etc/rc.d/init.d/nginx reload (code=exited, status=0/SUCCESS)
  Process: 28485 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=1/FAILURE)
 Main PID: 27022 (code=exited, status=0/SUCCESS)


Jun 21 10:37:14 vm10-0-0-19.ksc.com systemd[1]: Starting SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...
Jun 21 10:37:14 vm10-0-0-19.ksc.com nginx[28485]:
Starting nginx: nginx: [emerg] duplicate location "/" in /usr/local/nginx/conf/nginx.conf:74
Jun 21 10:37:14 vm10-0-0-19.ksc.com nginx[28485]: [FAILED]
Jun 21 10:37:14 vm10-0-0-19.ksc.com systemd[1]: nginx.service: control process exited, code=exited status=1
Jun 21 10:37:14 vm10-0-0-19.ksc.com systemd[1]: Failed to start SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server.

Jun 21 10:37:14 vm10-0-0-19.ksc.com systemd[1]: Unit nginx.service entered failed state.

Jun 21 10:37:14 vm10-0-0-19.ksc.com systemd[1]: nginx.service failed.

還有就是要開啟URL_MODEL = 2;