【问题标题】:Convert NGINX config to Apache将 NGINX 配置转换为 Apache
【发布时间】:2021-12-28 14:39:27
【问题描述】:

我通过 NGINX 运行 VueJS + FastAPI 应用程序,我有 2 条路由,“/”和“/api”

/ 提供在 localhost:5300 上运行的 vuejs 前端

/api 服务于在 localhost:5301 上运行的 fastapi 后端

我可以让它与以下 Nginx 配置一起工作,但需要将此配置移植到 Apache(Centos 7 上的 httpd 2.4.6)

我无法弄清楚 Apache 指令并不断收到 Invalid Host Header 错误,

nginx 配置

server {
    listen 80;
    server_name www.netportal.corp.com netportal.corp.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name www.netportal.corp.com netportal.corp.com;
    error_page 404 =301 https://www.corp.com;
    
    ssl_certificate /etc/ssl/certs/star.crt;
    ssl_certificate_key /etc/ssl/certs/star.key;
    
    location /api {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        rewrite ^/api/?(.*) /$1 break;
        proxy_redirect off;
        proxy_pass http://localhost:5301;
    }

    location / {
        proxy_pass http://0.0.0.0:5300/;
    }
}

这是我的 Apache 配置,我不断收到 Invalid Host Headers 错误

LoadModule  headers_module        /usr/lib64/httpd/modules/mod_headers.so
LoadModule  proxy_wstunnel_module /usr/lib64/httpd/modules/mod_proxy_wstunnel.so


#### rcuat

ProxyRequests Off
RewriteEngine On

ErrorLog /var/log/httpd/netportal_error.log

<VirtualHost *:80>
    ServerName netportal.corp.com
    ServerAlias netportal
    Redirect / https://netportal.corp.com
</VirtualHost>

<VirtualHost *:443>
    
    ServerName netportal.corp.com
    ServerAlias netportal

    SSLEngine On
    SSLProxyEngine On
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerExpire off
    SSLProxyCheckPeerName off

    SSLCertificateFile /etc/ssl/certs/star.cert
    SSLCertificateKeyFile /etc/ssl/certs/star.key

    Header always append X-Frame-Options SAMEORIGIN
    RequestHeader set X-SCHEME https
    ProxyPreserveHost On

    <Location /api/>
        ProxyPass http://localhost:5301/    
        ProxyPassReverse http://localhost:5301/
    </Location>

    
    <Location />
        RequestHeader set X-SCHEME https
        ProxyPass        / http://0.0.0.0:5300/ nocanon
        ProxyPassReverse / http://0.0.0.0:5300/
    </Location>
    # RequestHeader set X-Forwarded-Proto "https"

</VirtualHost>

有人可以帮助将 / 和 /api 指令转换为 Apache 语法吗?

是否有转换工具可以做到这一点?我似乎找不到任何有效的方法。

【问题讨论】:

    标签: apache vue.js nginx fastapi


    【解决方案1】:

    我稍微修正了你的配置。

    为代理到 http 后端定义了正确的模块。 删除了所有不需要的位置指令,这些指令只会让你真正想做的事情变得复杂。 添加了缺少的斜杠(根据经验,如果源以斜杠结尾,则目标也应始终以斜杠结尾)。 删除了 RewriteEngine,因为不需要/使用重写。

    LoadModule  headers_module /usr/lib64/httpd/modules/mod_headers.so
    # for proxying to http you just need mod_proxy and mod_proxy_http
    LoadModule proxy_module /usr/lib64/httpd/modules/mod_proxy.so
    LoadModule proxy_http_module /usr/lib64/httpd/modules/mod_proxy_http.so
    
    
    
    
    #### rcuat
    
    ProxyRequests Off
    
    ErrorLog /var/log/httpd/netportal_error.log
    
    <VirtualHost *:80>
        ServerName netportal.corp.com
        ServerAlias netportal
        #match trailing slashes in redirects and proxypass directives
        Redirect / https://netportal.corp.com/
    </VirtualHost>
    
    <VirtualHost *:443>
        ServerName netportal.corp.com
        ServerAlias netportal
    
        SSLEngine On
        #no https backend, related directives removed
    
        SSLCertificateFile /etc/ssl/certs/star.cert
        SSLCertificateKeyFile /etc/ssl/certs/star.key
    
        Header always append X-Frame-Options SAMEORIGIN
        RequestHeader set X-SCHEME https
        ProxyPreserveHost On
        # Proxypass should not go in location, it just makes a mess
        # Location is interpreted in the opposite order than proxypass too.
    
        # with proxypass most specific path must be defined first. More global last.
        # note that not matching source path and destiny path can cause issues with some backends depending how they behave, so examine backend behaviour carefully. because it definetly may be less troublesome to do proxypass /api/ http://backend/api/
    
    
        ProxyPass /api/ http://localhost:5301/
        ProxyPassReverse /api/ http://localhost:5301/
    
        ProxyPass        / http://0.0.0.0:5300/ nocanon
        ProxyPassReverse / http://0.0.0.0:5300/
    </VirtualHost>
    

    【讨论】:

    • 非常感谢我试试看!
    猜你喜欢
    • 1970-01-01
    • 2014-09-24
    • 2012-08-25
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2011-11-25
    相关资源
    最近更新 更多