【问题标题】:convert nginx conf to apache conf for proxypass将 nginx conf 转换为 proxypass 的 apache conf
【发布时间】:2020-12-02 04:35:17
【问题描述】:

我需要将此 nginx conf 转换为 apache conf。正如我试图转换,但未能得到它。

upstream apiUpstream {

  # 1. Set your port here
  server 127.0.0.1:8080;
  keepalive 64;
}

server {
  listen 80;
  listen 443 ssl;

  # 2. Set your API domain name here
  server_name yourdomain.com;

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # RequestHeader set X-Forwarded-Port "443"
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_pass http://apiUpstream/;  
    proxy_redirect off;
    proxy_read_timeout 240s;
  }

  # 3. SSL certificate set below
  # Here is added a sample certificate format of letsencrypt
  # You are free to remove it and configure yours here
  ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
  include /etc/letsencrypt/options-ssl-nginx.conf;
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

到 apache conf。

  ProxyRequests On
  ProxyPass   http://apiUpstream/ timeout=240
  ProxyPassReverse http://apiUpstream.com

但它不起作用,请帮我使它可行。而且我不明白什么是顶级上游 conf 以及如何将其转换为 apache。

编辑:

我收到了这个错误

peerjs.min.js:52 WebSocket connection to 'wss://api.example.com/peer/peerjs?key=peerjs&id=6yqD1MoKzkK&token=kp8g8w6pj2l' failed: Error during WebSocket handshake: 'Connection' header is missing

我已经尝试像这样更改域的 cpanel conf。

<Proxy balancer://apiUpstream/>

 BalancerMember http:// 127.0.0.1:8080;
 keepalive 64;
 ProxySet lbmethod=byrequests
</Proxy>

ProxyRequests On
ProxyPass / balancer://apiUpstream/
ProxyPassReverse / balancer://apiUpstream/

保存后,我无法从 WHM 重新启动 apache。它的说法就像无法加载平衡器成员一样。我不知道如何解决它。

截图

以及准确的错误信息

Syntax error on line 2 of /etc/apache2/conf.d/userdata/ssl/2_4/node/api.example.com/proxy_pass.conf: Dec 07 12:03:54 
ip-xx-xx-xxx-xx.ip.secureserver.net restartsrv_httpd[32106]: BalancerMember can not have a balancer name when defined in a location Dec 07 12:03:54

【问题讨论】:

  • 你得到什么错误...?
  • 我再次更新了我的问题,提供了确切的错误详细信息和内容
  • 你可以试试ProxyPass / http://127.0.0.1:8080/ ProxyPassReverse / http://127.0.0.1:8080/ RewriteEngine on RewriteCond %{HTTP:Upgrade} websocket [NC] RewriteCond %{HTTP:Connection} upgrade [NC] RewriteRule ^/?(.*) "ws://127.0.0.1:8080/$1" [P,L]
  • 这意味着您的软件正在尝试向不同的域发出 ajax 请求,并且该域不允许这样做。如果 api.example.com 是您的其他主机,则应添加 Header set Access-Control-Allow-Origin "*"

标签: apache nginx


【解决方案1】:

首先你需要加载模块的apache:

  • mod_proxy
  • mod_proxy_http
  • mod_proxy_wstunnel(在 httpd 2.4.5 及更高版本中可用)docs

在 ubuntu 中,您可以通过以下方式启用此功能:

a2enmod proxy
a2enmod proxy_http
a2enmod proxy_wstunnel

您正在尝试连接到您需要连接到节点应用程序的 websocket 的 WebSocket。

试试这样的:

<VirtualHost *:443>
  ServerName example.com
      
  ProxyPreserveHost On
  RewriteEngine On
     
  # If header Upgrade: websocket found, proxy to ws
  RewriteCond %{HTTP:Upgrade} =websocket [NC]  
  RewriteRule /(.*)           ws://localhost:8080/$1 [P,L]  
  
  # If header not found use http
  RewriteCond %{HTTP:Upgrade} !=websocket [NC]
  RewriteRule /(.*)           http://localhost:8080/$1 [P,L]

  
  # SSL config
  ...
  
</VirtualHost>

【讨论】:

  • 感谢您在这方面的帮助。不幸的是,当我尝试您的代码时。它在 api.example.com 上给了我 503 错误。我有机会从 stackoverflow 得到类似的东西。 stackoverflow.com/questions/33325364/… 这几乎是完全相反的。我尝试过这个。那也没有帮助我。
  • 503 错误表示无法访问底层服务器。我已经在 apache 中使用了带有播放框架应用程序(http + ws)的发布配置,并且它有效。我会在周末后检查并更新我的答案。
  • 谢谢@Pawel Hawro,我正在等待合适的解决方案。希望你们能成功。
猜你喜欢
  • 2014-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
相关资源
最近更新 更多