英文:
What is the Apache equivalent of nginx's try_files directive
问题
请帮忙,我对服务器配置很陌生。
我正在尝试在Apache的虚拟主机配置中编写等效于以下nginx服务器配置的内容。
<VirtualHost *:80>
ServerName myservername.test
DocumentRoot "/path/to/public/root/directory"
<Directory /path/to/public/root/directory/>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/web [OR]
RewriteCond %{REQUEST_URI} ^/api
RewriteRule . index.php [QSA,L]
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName myservername.test
ProxyPreserveHost On
ProxyPass / http://localhost:80/
ProxyPassReverse / http://localhost:80/
</VirtualHost>
但它没有起作用。我已经从头到尾调整了代码,但我的Apache服务器一直报错。我的Apache版本是2.4.52。
注意:我必须在Apache的虚拟主机配置中完成这个任务,而不是在.htaccess文件中。请提前帮助,谢谢。
英文:
Please help, i'm new to server configuration.
I'm trying to write the equivalent of the nginx server configuration below in Apache's virtual host configuration.
server {
root /path/to/public/root/directory;
server_name myservername.test;
location /api {
try_files $uri $uri/ /index.php?$query_string;
}
location /web {
try_files $uri $uri/ /index.php?$query_string;
}
location / {
proxy_pass http://localhost:80;
}
}
From what i have researched, I tried to do the following
<VirtualHost *:80>
ServerName myservername.test
DocumentRoot "/path/to/public/root/directory"
<Directory /path/to/public/root/directory/>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} "=/web" [OR]
RewriteCond %{REQUEST_FILENAME} "=/api"
RewriteRule . index.php [QSA, L]
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName myservername.test
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
but it did not work. I have tweaked the code from top to bottom but my apache server keeps throwing errors.
My Apache version is 2.4.52
Note: It is neccessary i do this in apache's vhost and not in the .htaccess file
Please help. thanks in advance.
答案1
得分: 0
I finally got it to work.
The first mistake I made was forgetting to enable mod_rewrite
, mod_proxy
, and mod_proxy_http
.
The second mistake was creating two vhosts with the same ServerName
value. Apache seems to ignore one of them.
After enabling those Apache modules, here is the final result that worked for me:
<VirtualHost *:80>
ServerName myservername.test
#
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond "%{REQUEST_URI}" "/web" [OR]
RewriteCond "%{REQUEST_URI}" "/api"
RewriteRule . index.php [QSA]
#
ProxyPass "/" "http://localhost:80/"
ProxyPassReverse "/" "http://localhost:80/"
</VirtualHost>
英文:
I finally got it to work.
The first mistake i made was forgetting to enable mod_rewrite
, mod_proxy
and mod_proxy_http
.
second mistake was i created two vhost with the same ServerName
value, apache seems to ignore one of them
After enabling those apache modules, here is the final result that worked for me
<VirtualHost *:80>
ServerName myservername.test
#
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond "%{REQUEST_URI}" "=/web" [OR]
RewriteCond "%{REQUEST_URI}" "=/api"
RewriteRule . index.php [QSA]
#
ProxyPass "/" "http://localhost:80/"
ProxyPassReverse "/" "http://localhost:80/"
</VirtualHost>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论