当 Django 不为 Web 服务器的根提供服务时如何引用 Django Rest Framework 路由器 URL
How to reference Django Rest Framework Router URLs when Django is not serving the root of the web server
我的 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/ 的网络服务器时,我确实得到了路由器 URLs,但是当我尝试访问其中一个实际的 URLs 时,我得到一个 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 应用程序仍会认为它位于站点的根目录。
我的 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/ 的网络服务器时,我确实得到了路由器 URLs,但是当我尝试访问其中一个实际的 URLs 时,我得到一个 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 应用程序仍会认为它位于站点的根目录。