Nginx

與 ProxyPass 一起使用時忽略 Apache 別名

  • August 12, 2021

我正在嘗試在我的伺服器上安裝yotter,並且我想使用 apache 而不是 nginx。安裝指南只列出了一個我試圖“翻譯”的 nginx 配置文件。

配置是三個不同的代理路由,第一個通向 docker 容器,第二個 ( /static/ ) 到帶有樣式表和類似內容的目錄,最後一個到 unix 域套接字。

就我而言,第一條路線似乎有效(我可以訪問該站點),但是對靜態資源的每個請求都返回一個 404。看起來每個請求都使用 docker ProxyPass 指令(無論順序如何:如果我把那個最後一個(見下文),它仍然是唯一使用的)。

這是創建的日誌文件的(一部分):

yotter.domain.tld - - [12/Aug/2021:21:14:21 +0200] "GET /static/favicons/favicon.ico HTTP/1.1" 404 1935 file=proxy:http://127.0.0.1:5000/static/favicons/favicon.ico
yotter.domain.tld - - [12/Aug/2021:21:15:54 +0200] "GET /index HTTP/1.1" 200 1126 file=proxy:http://127.0.0.1:5000/index
yotter.domain.tld - - [12/Aug/2021:21:15:54 +0200] "GET /static/semantic/semantic.min.css HTTP/1.1" 404 1935 file=proxy:http://127.0.0.1:5000/static/semantic/semantic.min.css

這可能是什麼原因?

這是 nginx 配置:

server {
   listen 80;
   server_name <example.com>; # ChangeME
   access_log off;

   location / {
       proxy_pass http://127.0.0.1:5000;
       proxy_http_version 1.1;
       proxy_set_header Connection "";
   }

   location /static/ {
       root /home/ubuntu/Yotter/app/; # Change this depending on where you clone Yotter
       sendfile on;
       aio threads=default;
   }

   location ~ (^/videoplayback$|/videoplayback/|/vi/|/a/) {
       proxy_pass http://unix:/var/run/ytproxy/http-proxy.sock;
       add_header Access-Control-Allow-Origin *;
       sendfile on;
       tcp_nopush on;
       aio_write on;
       aio threads=default;
       directio 512;
       proxy_http_version 1.1;
       proxy_set_header Connection "";
   }
}

這是我到目前為止所擁有的:

<VirtualHost *:443>
       ServerName yotter.domain.tld

       LogFormat "%v %l %u %t \"%r\" %>s %b file=%f" commonvhost
       CustomLog ${APACHE_LOG_DIR}/yotter_access.log commonvhost

       SSLEngine on
       SSLCertificateFile /etc/letsencrypt/live/domain.tld/fullchain.pem
       SSLCertificateKeyFile /etc/letsencrypt/live/domain.tld/privkey.pem

       <Location "/">
               ProxyPass http://127.0.0.1:5000/
               ProxyPassReverse http://127.0.0.1:5000/
       </Location>


       <Location "/static">
               Alias "/var/www/Yotter/app/static"
       </Location>

       <Directory "/var/www/Yotter/app/">
               Require all granted
       </Directory>


       <LocationMatch (^/videoplayback$|/videoplayback/|/vi/|/a/)>
               ProxyPass unix:///var/run/ytproxy/http-proxy.sock
               ProxyPassReverse unix:///var/run/ytproxy/http-proxy.sock
               Header add Acces-Control-Allow-Origin: *
       </LocationMatch>

</VirtualHost>

或者

<VirtualHost *:443>
       ServerName yotter.domain.tld

       LogFormat "%v %l %u %t \"%r\" %>s %b file=%f" commonvhost
       CustomLog ${APACHE_LOG_DIR}/yotter_access.log commonvhost

       SSLEngine on
       SSLCertificateFile /etc/letsencrypt/live/domain.tld/fullchain.pem
       SSLCertificateKeyFile /etc/letsencrypt/live/domain.tld/privkey.pem


       Alias "/static" "/var/www/Yotter/app/static"

       <Directory "/var/www/Yotter/app/">
               Require all granted
       </Directory>


       <LocationMatch (^/videoplayback$|/videoplayback/|/vi/|/a/)>
               ProxyPass unix:///var/run/ytproxy/http-proxy.sock
               ProxyPassReverse unix:///var/run/ytproxy/http-proxy.sock
               Header add Acces-Control-Allow-Origin: *
       </LocationMatch>


       ProxyPass / http://127.0.0.1:5000/
       ProxyPassReverse / http://127.0.0.1:5000/

</VirtualHost>

您需要從代理中排除靜態!

ProxyPass "/static" "!"

這應該出現在配置中的代理命令之前。

或者:

<Location "/static">
   ProxyPass "!"
</Location>

另請參閱ProxyPass 官方文件

引用自:https://serverfault.com/questions/1074384