Apache的等效指令是什么?

huangapple go评论56阅读模式
英文:

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

&lt;VirtualHost *:80&gt;
	    ServerName myservername.test
        DocumentRoot &quot;/path/to/public/root/directory&quot;

	    &lt;Directory /path/to/public/root/directory/&gt;
                RewriteEngine on
                RewriteCond %{REQUEST_FILENAME} !-d  
	            RewriteCond %{REQUEST_FILENAME} !-f

                RewriteCond %{REQUEST_FILENAME} &quot;=/web&quot; [OR]	
 	            RewriteCond %{REQUEST_FILENAME} &quot;=/api&quot;

	            RewriteRule . index.php [QSA, L]
        &lt;/Directory&gt;	
&lt;/VirtualHost&gt;


&lt;VirtualHost *:80&gt;
	    ServerName myservername.test
 	    ProxyPreserveHost On
 	    ProxyPass / http://localhost:8080/
	    ProxyPassReverse / http://localhost:8080/
&lt;/VirtualHost&gt;

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

&lt;VirtualHost *:80&gt;
    ServerName myservername.test
	#
	RewriteEngine on
	RewriteCond %{REQUEST_FILENAME} !-d  
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond &quot;%{REQUEST_URI}&quot; &quot;=/web&quot; [OR]    
    RewriteCond &quot;%{REQUEST_URI}&quot; &quot;=/api&quot;
    RewriteRule . index.php [QSA]
	#
    ProxyPass &quot;/&quot;  &quot;http://localhost:80/&quot;
    ProxyPassReverse &quot;/&quot;  &quot;http://localhost:80/&quot;
&lt;/VirtualHost&gt;

huangapple
  • 本文由 发表于 2023年2月14日 05:55:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定