Re: AllegroServe : using 2 servers on the same machine
John Foderaro <[email protected]> Sun, 28 Dec 2003 19:43:23 -0800
| Newsgroups | gmane.lisp.open-source.franz |
|---|---|
| Message-ID | <[email protected]> |
You can get the effect you want by using the proxy and redirect hooks
in AllegroServe.
You put one server on port 80 and the other on some other port.
The server on port 80 serves its content and if it sees a request
for content on the other server it changes the request into a
proxy request to the other server, and then it handles that proxy
request.
In order for this to work it must be possible for the server on
port 80 to recognize a request the other server can handle.
Here's an example of this at work.
To make it simple I've run both servers in the same lisp although
this isn't necessary.
I've run the servers on ports 5080 and 5081.
you'll note that four url's work
http://localhost:5080/foo/page1
http://localhost:5080/foo/page2
http://localhost:5081/bar/page1
http://localhost:5081/bar/page2
and then due to the request filter I added you can also access
the "/bar/.." pages at
http://localhost:5080/bar/page1
http://localhost:5080/bar/page2
thus a user need only know about the server on port 5080 to access
everything.
;;;;;;;;;;;;;;; the code ;;;;;;;;;;;;;;;;;;;;;;;;;;
(require :aserve)
(defpackage :user (:use :net.aserve :net.html.generator))
(in-package :user)
;------------
; setup server for the "/foo/... " pages
; this is also setup as a proxy server so it can also serve
; the /bar pages
(defvar *wserv1*
(net.aserve:start :port 5080 :server :new :proxy t))
(publish :path "/foo/page1" :server *wserv1*
:content-type "text/plain"
:function #'(lambda (req ent)
(with-http-response (req ent)
(with-http-body (req ent)
(html "foo's page 1")))))
(publish :path "/foo/page2" :server *wserv1*
:content-type "text/plain"
:function #'(lambda (req ent)
(with-http-response (req ent)
(with-http-body (req ent)
(html "foo's page 2")))))
;-----------------
;; serve the /bar/.. pages on port 5081
(defvar *wserv2*
(net.aserve:start :port 5081 :server :new))
(publish :path "/bar/page1" :server *wserv2*
:content-type "text/plain"
:function #'(lambda (req ent)
(with-http-response (req ent)
(with-http-body (req ent)
(html "bar's page 1")))))
(publish :path "/bar/page2" :server *wserv2*
:content-type "text/plain"
:function #'(lambda (req ent)
(with-http-response (req ent)
(with-http-body (req ent)
(html "bar's page 2")))))
(defun redirect-to-other-server (req)
;;
;; any request for "/bar/..." turn into a proxy request
;; to the server for the bar pages on port 5081
;;
(format t "raw ~s~%req-uri ~s, header ~s~%"
(request-raw-uri req)
(request-uri req)
(header-slot-value req :host))
(let ((raw-uri (request-raw-uri req))
(uri (request-uri req)))
(if* (match-regexp "^/bar/" (net.uri:uri-path uri))
then (format t "doing the redirect~%")
(setf (net.uri:uri-port uri) 5081)
(setf (header-slot-value req :host)
(format nil "~a:5081" (net.uri:uri-host uri)))
; turn the raw uri into a proxy uri
(setf (net.uri:uri-scheme raw-uri) (net.uri:uri-scheme uri))
(setf (net.uri:uri-host raw-uri) (net.uri:uri-host uri))
(setf (net.uri:uri-port raw-uri) 5081)
(format t "After~%raw ~s~%req-uri ~s, header ~s~%"
(request-raw-uri req)
(request-uri req)
(header-slot-value req :host)))
t))
(pushnew 'redirect-to-other-server (wserver-filters *wserv1*))