Nginx:从根目录的子目录中提供一个应用程序

我正在尝试将为Apache编写的应用程序(或.htaccess-files)迁移到Nginx,而且我遇到了一个问题,我无法将其包裹起来。 我来自Apach环境,所以这对我来说是相当新的。

我有以下文件夹结构:

/var/www/mydomain.tld/ └── web ├── error ├── favicon.ico ├── my_application ├── index.html └── robots.txt 

web是mydomain.tld的www-root文件夹,我的应用程序位于/web/my_aplication/我想在www.my_domain.tld被调用时在/ my_application中为应用程序提供服务。

标准指令是由ISPConfig自动创build的,但我想我需要添加几行,但我不知道我做错了什么。 这里是相关的部分(我猜):

 server { listen 1.2.3.4:80; server_name my_domain.tld; root /var/www/mydomain.tld/web; ## force www - added this myself if ($http_host !~ "^www\.") { rewrite ^(.*)$ http://www.$http_host$request_uri redirect; } ## rewrite to /my_application/ and last if ($http_host ~* "^www.my_domain.tld$") { rewrite ^/(.+)$ /my_application/$1 last; } index index.php index.html index.htm index.cgi index.pl index.xhtml; error_page 400 /error/400.html; # .... recursive_error_pages on; location = /error/400.html { internal; } ## .... **** this part here is explained below **** } 

除了force-www-part之外,这些指令是自动生成的,实际上应该可以正常工作。

但是现在来翻译my_application文件夹中的.htaccess-rules。 我使用了htaccess到nginx转换器,并得到了location / { ... }一些Nginx指令。 我将位置更改为/ my_application,并将指令放在上面提到的位置:

 locaction /my_application { rewrite (.*/)?info/([A-Za-z0-9_-]+)\.html.* /shop_content.php?gm_boosted_content=$2&$query_string break; rewrite (.*/)?([A-Za-z0-9_-]+)\.html /product_info.php?gm_boosted_product=$2&$query_string break; rewrite (.*/)?([A-Za-z0-9_-]+)/?.* /index.php?gm_boosted_category=$2&$query_string break; } ## These are automatically generated directives for the root-folder (web, I guess). ## Please note that I have placed my own directives above them, I don't know ## if that mattes though. location ~ \.php$ { try_files $uri =404; include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9010; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name; fastcgi_intercept_errors on; } 

所以,实际上没有什么特别的,但我不能得到它的工作… 反而会发生 :index.php正在下载(不执行)。 但是我不太清楚为什么,因为这个方法和我通过Google发现的方法非常相似。 但是我可能错过了一些东西。

你正在重写而不是最后使用中断标志。 Break意味着Nginx应该重写内部URI,而不是重新评估位置指令,因此它永远不会传递给PHP,而是发送给下载。

另外它下载和不显示的原因是你没有为php文件添加MIMEtypes,你的default_type被configuration为application / octet-stream。