gpt4 book ai didi

python - 当 Django 不为 Web 服务器的根提供服务时,如何引用 Django Rest Framework 路由器 URL

转载 作者:行者123 更新时间:2023-12-01 04:48:44 25 4
gpt4 key购买 nike

我的 API 运行良好,但 DJango 服务于 Apache 后面的整个文档根目录。团队决定他们希望通过 apache 直接从根(非 API)提供一些服务,因此我将 DJango WSGI 引用从/推送到/api。

来自:

WSGIScriptAlias / /opt/org/myproj/myapp/wsgi.py

致:

WSGIScriptAlias /api/ /opt/org/myproj/myapp/wsgi.py

主要 urls.py 来自:

urlpatterns = patterns('',
url(r'^api/', include(router.urls)),

至:

urlpatterns = patterns('',
url(r'^', include(router.urls)),

问题是,当我访问/api/处的网络服务器时,我确实获得了路由器 URL,但是当我尝试访问其中一个实际 URL 时,我收到了 404。它实际上并没有处理任何子 URL 引用。

正确点击/api/会给出:

{
address: "https://172.17.100.7/api/address/",
chassis: "https://172.17.100.7/api/chassis/",
configurationfile: "https://172.17.100.7/api/configurationfile/",
job: "https://172.17.100.7/api/job/",
node: "https://172.17.100.7/api/node/",
operatingsystem: "https://172.17.100.7/api/operatingsystem/"
}

点击/api/address 给出:

Not Found

The requested URL /api/address/ was not found on this server.

Apache/2.2.22 (Debian) Server at 172.17.100.7 Port 443

有人可以提供一些关于在这种情况下应该如何引用 URL 的见解吗?我以为这将是一个非常简单的更改,但我不知所措。

编辑:这是完整的 apache 配置。

WSGIScriptAlias /api/ /opt/org/myproj/myapp/wsgi.py

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

<VirtualHost *:443>

ServerAdmin webmaster@localhost
DocumentRoot /opt/org/myproj/root

<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>

<Directory /opt/org/myproj/root/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

Alias /repo /opt/myorg/myproj/repo/

<Directory /opt/hp/moonshot/repo/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

ErrorLog /opt/myorg/myproj/logs/error.log
LogLevel warn
CustomLog /opt/myorg/myproj/logs/access.log combined

<Directory /opt/myorg/myproj/myapp>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

# SSL Options
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
BrowserMatch "MSIE [2-6]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
</VirtualHost>

最佳答案

根据内存,使用:

WSGIScriptAlias /api/ /opt/org/myproj/myapp/wsgi.py/api/

将你的 Django 东西恢复到原来的样子。

这只会拦截“/api/”的子 URL,但 WSGI 应用程序仍会认为它位于站点的根目录。

关于python - 当 Django 不为 Web 服务器的根提供服务时,如何引用 Django Rest Framework 路由器 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28868944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com