ubuntu Apache在子目录前缀上运行API

e3bfsja2  于 2023-08-03  发布在  Apache
关注(0)|答案(1)|浏览(106)

Ubuntu 22.04| Apache 2.4.52
我按照https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-6.0上的步骤操作
我让它工作,以便.NET Core Web API在http://127.0.0.1:5000上运行。
我的apache配置目前在/etc/apache2/sites-available中看起来像这样

<VirtualHost *:*>
    RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}s
</VirtualHost>

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    ServerName mydomain.test
    Header set Access-Control-Allow-Origin "*"
    Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
    Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =mydomain.test
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

字符串
具有相同名称但带有后缀“-l-ssl”的配置如下所示

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ProxyPreserveHost On
        ProxyPass / http://127.0.0.1:5000/
        ProxyPassReverse / http://127.0.0.1:5000/
        ServerName mydomain.test
        ErrorLog ${APACHE_LOG_DIR}/api-error.log
        CustomLog ${APACHE_LOG_DIR}/api-access.log common
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/mydomain.test/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.test/privkey.pem
    </VirtualHost>
</IfModule>


这是所有工作,我可以调用我的API对我的根示例https://mydomain.test/ping
但是,我希望我的API在子目录/api上运行,我的Nuxt 3应用程序在根目录上运行。我用运行在http://localhost:3000上的pm2做了这件事。如果我在配置中将http://127.0.0.1:5000/更改为http://localhost:3000,我的Nuxt 3应用程序就会显示出来。
我该怎么做?我是Apache和Ubuntu的新手,我希望有人能帮助我解决我面临的这些麻烦,让它工作起来。

bq9c1y66

bq9c1y661#

我做了一些研究,我使它的工作像一个魅力与以下。

<VirtualHost *:*>
    RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}s
</VirtualHost>

<VirtualHost *:80>
    ProxyRequests Off
    ProxyPreserveHost On
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass /api/ http://127.0.0.1:5000/
    ProxyPassReverse /api/ http://127.0.0.1:5000/
    ServerName mydomain.test
    
    Header set Access-Control-Allow-Origin "*"
    Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
    Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomain.test
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

字符串
感谢rene marxis post

相关问题