RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 673245
Accepted
Eugen Eray
Eugen Eray
Asked:2020-05-31 16:44:38 +0000 UTC2020-05-31 16:44:38 +0000 UTC 2020-05-31 16:44:38 +0000 UTC

删除 nginx 中的 .php 扩展名

  • 772

大家好!一个常见的问题,我看了很多关于这个的,但是我不明白nifiga。

我在网站上有以下类型的所有链接:所有文件和子目录中的文件都不是等等contactinf。contactinf.php用 PHP + .htaccess 代码实现:

PHP:

if(stristr($_SERVER['REQUEST_URI'], ".php")){
   $currentPage = $_SERVER['PHP_SELF'];
   $shortCut = str_replace(".php", "", $currentPage);
   header("Location:".$shortCut);
}

.htaccess:

Options +MultiViews

这在 nginx 中不起作用,由于我没有.php在链接末尾指定,我所有的链接都停止工作。如有必要,我将附上完整的配置nginx.conf文件:

#load_module "modules/ngx_http_image_filter_module.so";
#load_module "modules/ngx_http_perl_module.so";
#load_module "modules/ngx_http_xslt_filter_module.so";
#load_module "modules/ngx_mail_module.so";
#load_module "modules/ngx_stream_module.so";

user                         root admin;
worker_processes             4;

events {
    worker_connections       27000;
}

http {
    include       		    mime.types;
    default_type             text/html;
	charset                  utf-8;
	error_page 500           http://localhost/errors/undefined;
    error_page 404           http://localhost/errors/undefined;
    error_page 403           http://localhost/errors/undefined;
	gzip                     on;
	gzip_types               text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon image/png image/jpeg;
    
    sendfile                 on;

	server {
		listen               80 default_server;

		# MAMP DOCUMENT_ROOT !! Don't remove this line !!
		root                 "/Users/eugen/Documents/HTML,CSS,PHP, JavaScript,XML lessons/ER website/en/";

		access_log           /Applications/MAMP/logs/nginx_access.log;
		error_log            /Applications/MAMP/logs/nginx_error.log;

		location / {
               index index.html index.htm index.php; 
		}
	  
	    
		location ~* /MAMP[^-]*(.*)$ {
			root             /Applications/MAMP/bin;
			index            index.php;

			location ~ \.php$ {
				try_files        $uri =404;
				fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
				fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
				include          fastcgi_params;
			}
		}

		location ~* /phpMyAdmin(.*)$ {
			root             /Applications/MAMP/bin;
			index            index.php;

			location ~ \.php$ {
				try_files        $uri =404;
				fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
				fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
				include          fastcgi_params;
			}
		}

		location ~* /phpPgAdmin(.*)$ {
			root             /Applications/MAMP/bin;
			index            index.php;

			location ~ \.php$ {
				try_files        $uri =404;
				fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
				fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
				include          fastcgi_params;
			}
		}
		
		location ~* /phpLiteAdmin(.*)$ {
			root             /Applications/MAMP/bin;
			index            index.php;

			location ~ \.php$ {
				try_files        $uri =404;
				fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
				fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
				include          fastcgi_params;
			}
		}
		
		location ~* /SQLiteManager(.*)$ {
			root             /Applications/MAMP/bin;
			index            index.php;

			location ~ \.php$ {
				try_files        $uri =404;
				fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
				fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
				include          fastcgi_params;
			}
		}

		location ~* /adminer(.*)$ {
			root             /Applications/MAMP/bin;
			index            adminer.php;

			location ~ \.php$ {
				try_files        $uri =404;
				fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
				fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
				include          fastcgi_params;
			}
		}
		
		    location ~ .(ini|phps|log|txt|rtf)$ {
              deny all;
              allow 192.168.0.101;
              allow 192.168.0.100;
              allow 192.168.0.102;
              allow 93.75.31.82;
              allow 127.0.0.1;
         }
		
		location /icons {
			alias /Applications/MAMP/Library/icons;
			autoindex on;
		}
		
		location /favicon.ico {
			alias /Applications/MAMP/bin/favicon.ico;
            # log_not_found off;
            # access_log off;
		}

		location ~ \.php$ {
			try_files        $uri =404;
			fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
			fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
			include          fastcgi_params;
		}

		location ~ /\. {
			deny all;
		}

		# location ~* \.(gif|jpg|png|pdf)$ {
        #	expires          30d;
		# }

   		# location = /robots.txt {
        #	allow all;
        #	log_not_found off;
        #	access_log off;
   		# }

	    # location ~* \.(txt|log)$ {
        #	allow 127.0.0.1;
        #	deny all;
   		# }
   		
   		# location ~ \..*/.*\.php$ {
        #	              return 403;
   		# }

		location /nginx_status {
			stub_status      on;
			access_log       off;
			allow            127.0.0.1;
			deny			 all;
		}
	}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

	#	 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

http {
    include       		    mime.types;
    default_type             text/html;
	charset                  utf-8;
	error_page 500           http://localhost/errors/undefined;
    error_page 404           http://localhost/errors/undefined;
    error_page 403           http://localhost/errors/undefined;
	gzip                     on;
	gzip_types               text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon image/png image/jpeg;
    
    sendfile                 on;

	server {
		listen               80 default_server;

		# MAMP DOCUMENT_ROOT !! Don't remove this line !!
		root                 "/Users/eugen/Documents/HTML,CSS,PHP, JavaScript,XML lessons/ER website/en/";

		access_log           /Applications/MAMP/logs/nginx_access.log;
		error_log            /Applications/MAMP/logs/nginx_error.log;

		location / {
               index index.html index.htm index.php;


             #Deny access for commom bad bots spiders and robots
			
			if (!-e $request_filename){
                return 403;}
			if ($query_string ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})"){
                 return 403;}
            if ($query_string ~ "FILES(=|\[|\%[0-9A-Z]{0,2})"){
              return 403;}
            if ($query_string ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})"){
              return 403;}
			if ($http_user_agent ~* "^BlackWidow"){
              return 403;}
			if ($http_user_agent ~* "^Bot\ mailto:craftbot@yahoo.com"){
              return 403;}
			if ($http_user_agent ~* "^ChinaClaw"){
              return 403;}
			if ($http_user_agent ~* "^Custo"){
              return 403;}
			if ($http_user_agent ~* "^DISCo"){
              return 403;}
			if ($http_user_agent ~* "^Download\ Demon"){
              return 403;}
			if ($http_user_agent ~* "^eCatch"){
              return 403;}
			if ($http_user_agent ~* "^EirGrabber"){
              return 403;}
			if ($http_user_agent ~* "^EmailSiphon"){
              return 403;}
			if ($http_user_agent ~* "^EmailWolf"){
              return 403;}
			if ($http_user_agent ~* "^Express\ WebPictures"){
              return 403;}
			if ($http_user_agent ~* "^ExtractorPro"){
              return 403;}
			if ($http_user_agent ~* "^EyeNetIE"){
              return 403;}
			if ($http_user_agent ~* "^FlashGet"){
              return 403;}
			if ($http_user_agent ~* "^GetRight"){
              return 403;}
			if ($http_user_agent ~* "^GetWeb!"){
              return 403;}
			if ($http_user_agent ~* "^Go!Zilla"){
              return 403;}
			if ($http_user_agent ~* "^Go-Ahead-Got-It"){
              return 403;}
			if ($http_user_agent ~* "^GrabNet"){
              return 403;}
			if ($http_user_agent ~* "^Grafula"){
              return 403;}
			if ($http_user_agent ~* "^HMView"){
              return 403;}
			if ($http_user_agent ~* "HTTrack"){
              return 403;}
			if ($http_user_agent ~* "^Image\ Stripper"){
              return 403;}
			if ($http_user_agent ~* "^Image\ Sucker"){
              return 403;}
			if ($http_user_agent ~* "Indy\ Library"){
              return 403;}
			if ($http_user_agent ~* "^InterGET"){
              return 403;}
			if ($http_user_agent ~* "^Internet\ Ninja"){
              return 403;}
			if ($http_user_agent ~* "^JetCar"){
              return 403;}
			if ($http_user_agent ~* "^JOC\ Web\ Spider"){
              return 403;}
			if ($http_user_agent ~* "^larbin"){
              return 403;}
			if ($http_user_agent ~* "^LeechFTP"){
              return 403;}
			if ($http_user_agent ~* "^Mass\ Downloader"){
              return 403;}
			if ($http_user_agent ~* "^MIDown\ tool"){
              return 403;}
			if ($http_user_agent ~* "^Mister\ PiX"){
              return 403;}
			if ($http_user_agent ~* "^Navroad"){
              return 403;}
			if ($http_user_agent ~* "^NearSite"){
              return 403;}
			if ($http_user_agent ~* "^NetAnts"){
              return 403;}
			if ($http_user_agent ~* "^NetSpider"){
              return 403;}
			if ($http_user_agent ~* "^Net\ Vampire"){
              return 403;}
			if ($http_user_agent ~* "^NetZIP"){
              return 403;}
			if ($http_user_agent ~* "^Octopus"){
              return 403;}
			if ($http_user_agent ~* "^Offline\ Explorer"){
              return 403;}
			if ($http_user_agent ~* "^Offline\ Navigator"){
              return 403;}
			if ($http_user_agent ~* "^PageGrabber"){
              return 403;}
			if ($http_user_agent ~* "^Papa\ Foto"){
              return 403;}
			if ($http_user_agent ~* "^pavuk"){
              return 403;}
			if ($http_user_agent ~* "^pcBrowser"){
              return 403;}
			if ($http_user_agent ~* "^RealDownload"){
              return 403;}
			if ($http_user_agent ~* "^ReGet"){
              return 403;}
			if ($http_user_agent ~* "^SiteSnagger"){
              return 403;}
			if ($http_user_agent ~* "^SmartDownload"){
              return 403;}
			if ($http_user_agent ~* "^SuperBot"){
              return 403;}
			if ($http_user_agent ~* "^SuperHTTP"){
              return 403;}
			if ($http_user_agent ~* "^Surfbot"){
              return 403;}
			if ($http_user_agent ~* "^tAkeOut"){
              return 403;}
			if ($http_user_agent ~* "^Teleport\ Pro"){
              return 403;}
			if ($http_user_agent ~* "^VoidEYE"){
              return 403;}
			if ($http_user_agent ~* "^Web\ Image\ Collector"){
              return 403;}
			if ($http_user_agent ~* "^Web\ Sucker"){
              return 403;}
			if ($http_user_agent ~* "^WebAuto"){
              return 403;}
			if ($http_user_agent ~* "^WebCopier"){
              return 403;}
			if ($http_user_agent ~* "^WebFetch"){
              return 403;}
			if ($http_user_agent ~* "^WebGo\ IS"){
              return 403;}
			if ($http_user_agent ~* "^WebLeacher"){
              return 403;}
			if ($http_user_agent ~* "^WebReaper"){
              return 403;}
			if ($http_user_agent ~* "^WebSauger"){
              return 403;}
			if ($http_user_agent ~* "^Website\ eXtractor"){
              return 403;}
			if ($http_user_agent ~* "^Website\ Quester"){
              return 403;}
			if ($http_user_agent ~* "^WebStripper"){
              return 403;}
			if ($http_user_agent ~* "^WebWhacker"){
              return 403;}
			if ($http_user_agent ~* "^WebZIP"){
              return 403;}
			if ($http_user_agent ~* "^Wget"){
              return 403;}
			if ($http_user_agent ~* "^Widow"){
              return 403;}
			if ($http_user_agent ~* "^WWWOFFLE"){
              return 403;}
			if ($http_user_agent ~* "^Xaldon\ WebSpider"){
              return 403;}
			if ($http_user_agent ~* "^Zeus"){
              return 403;}  
			  
			rewrite ^(/.*)\.php(\?.*)?$ $1$2 permanent;
            rewrite ^/(.*)/$ /$1 permanent;
            try_files $uri/index.php $uri.php $uri/ $uri =404;  
		}
	
		location ~* /MAMP[^-]*(.*)$ {
			root             /Applications/MAMP/bin;
			index            index.php;

			location ~ \.php$ {
				try_files        $uri =404;
				fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
				fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
				include          fastcgi_params;
			}
		}

		location ~* /phpMyAdmin(.*)$ {
			root             /Applications/MAMP/bin;
			index            index.php;

			location ~ \.php$ {
				try_files        $uri =404;
				fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
				fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
				include          fastcgi_params;
			}
		}

		location ~* /phpPgAdmin(.*)$ {
			root             /Applications/MAMP/bin;
			index            index.php;

			location ~ \.php$ {
				try_files        $uri =404;
				fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
				fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
				include          fastcgi_params;
			}
		}
		
		location ~* /phpLiteAdmin(.*)$ {
			root             /Applications/MAMP/bin;
			index            index.php;

			location ~ \.php$ {
				try_files        $uri =404;
				fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
				fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
				include          fastcgi_params;
			}
		}
		
		location ~* /SQLiteManager(.*)$ {
			root             /Applications/MAMP/bin;
			index            index.php;

			location ~ \.php$ {
				try_files        $uri =404;
				fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
				fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
				include          fastcgi_params;
			}
		}

		location ~* /adminer(.*)$ {
			root             /Applications/MAMP/bin;
			index            adminer.php;

			location ~ \.php$ {
				try_files        $uri =404;
				fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
				fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
				include          fastcgi_params;
			}
		}
		
		    location ~ .(ini|phps|log|txt|rtf)$ {
              deny all;
              allow 192.168.0.101;
              allow 192.168.0.100;
              allow 192.168.0.102;
              allow 93.75.31.82;
              allow 127.0.0.1;
         }
		
		location /icons {
			alias /Applications/MAMP/Library/icons;
			autoindex on;
		}
		
		location /favicon.ico {
			alias /Applications/MAMP/bin/favicon.ico;
            # log_not_found off;
            # access_log off;
		}

		location ~ \.php$ {
			try_files        $uri =404;
			fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
			fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
			include          fastcgi_params;
		}

		location ~ /\. {
			deny all;
		}

		# location ~* \.(gif|jpg|png|pdf)$ {
        #	expires          30d;
		# }

   		# location = /robots.txt {
        #	allow all;
        #	log_not_found off;
        #	access_log off;
   		# }

	    # location ~* \.(txt|log)$ {
        #	allow 127.0.0.1;
        #	deny all;
   		# }
   		
   		# location ~ \..*/.*\.php$ {
        #	              return 403;
   		# }

		location /nginx_status {
			stub_status      on;
			access_log       off;
			allow            127.0.0.1;
			deny			 all;
		}
	}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

	#	 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

php
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Eugen Eray
    2020-06-03T02:53:57Z2020-06-03T02:53:57Z

    Mdaa .. 答案竟然是:

    location / {
                   index index.html index.htm index.php; 
                   try_files $uri $uri/ @rewrite;
            }
    
            location ~ \.php$ {
                try_files        $uri =404;
                fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
                fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include          fastcgi_params;
            }
    
            location @rewrite {
                   rewrite ^ $uri.php last;
            }
    
    • 3
  2. Naumov
    2020-06-02T21:27:28Z2020-06-02T21:27:28Z

    删除 ngix 中所有不必要的并写一个 try files 部分

     location / {
            try_files $uri $uri/ /rout.php$is_args$args;
     }
    

    在 route.php 中我们写:

    if(stristr($_SERVER['REQUEST_URI'], ".php")){
       $currentPage = $_SERVER['PHP_SELF'];
       $shortCut = str_replace(".php", "", $currentPage);
       echo include($shortCut);
    }
    

    但这是一个抄写员,多么骇人听闻,最好将 php 添加到所有链接

    • -1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5