Nginx configures virtual domain names and hides public and index.php paths
One, phpStudy creates a website
- Set access domain name
- Root directory fill in the project directory to the public directory
- Pay attention to check the synchronization hosts
- Confirm PHP version> 7.1.0
- Restart the Nginx server
- Can be accessed directly
http://tp6.com/index.php/index/test
(index/test is the test method written under the index controller)
Two, hide index.php
If you want to hide index.php, direct access http://tp6.com/index/test
you need to configure Nginx server:
Method 1: vhosts.conf
Click the vhosts.conf
configuration file of the newly created website and add the code:
if (!-e $request_filename){
rewrite ^/index.php(.*)$ /index.php?s=$1 last;
rewrite ^/(.*)$ /index.php?s=$1 last;
}
the complete document:
server {
listen 80;
server_name tp6.com;
root "D:/phpstudy_pro/WWW/tp6/public";
location / {
index index.php index.html error/index.html;
if (!-e $request_filename){
rewrite ^/index.php(.*)$ /index.php?s=$1 last;
rewrite ^/(.*)$ /index.php?s=$1 last;
}
error_page 400 /error/400.html;
error_page 403 /error/403.html;
error_page 404 /error/404.html;
error_page 500 /error/500.html;
error_page 501 /error/501.html;
error_page 502 /error/502.html;
error_page 503 /error/503.html;
error_page 504 /error/504.html;
error_page 505 /error/505.html;
error_page 506 /error/506.html;
error_page 507 /error/507.html;
error_page 509 /error/509.html;
error_page 510 /error/510.html;
include D:/phpstudy_pro/WWW/tp6/public/nginx.htaccess;
autoindex off;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9004;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
If you are not using an integrated environment, you can copy the above code to the Nginx related configuration file.
配置完成,重启Nginx服务器。
Now it can be accessed in two ways:
http://tp6.com/index/test
http://tp6.com/index.php/index/test
Method 2: phpStudy panel configuration pseudo-static
Site list> Management> Modify> Pseudo-static add the following code:
Method three: add the project root nginx.htaccess
file
Add code:
if (!-e $request_filename){
rewrite ^/index.php(.*)$ /index.php?s=$1 last;
rewrite ^/(.*)$ /index.php?s=$1 last;
}