Nginx + Apache mod_wsgi,很難理解如何配置伺服器
我已經嘗試在我的 Ubuntu 12.04 上設置開發環境太久了。我的意圖是設置一切,就像我稍後在生產環境中必須做的那樣,所以我按照本教程介紹瞭如何使用 Apache 2 將 Nginx 設置為反向代理。
我一直在搜尋,我的問題與這個問題非常相似,但那裡沒有給出答案,我無法評論它以添加我的一些研究。
讓我們進入文件:
阿帕奇和wsgi
首先,我從 apt-get 安裝了 mod_wsgi,然後在 /etc/apache2/sites-available 中創建了一個 mysite 文件:
<VirtualHost *:8080> ServerAdmin mymail@mymail.com ServerName mysite.dev ErrorLog /path/to/developmentfolder/mysite/logs/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel debug CustomLog /path/to/developmentfolder/mysite/logs/access.log combined WSGIDaemonProcess mysite WSGIProcessGroup mysite WSGIScriptAlias / /path/to/developmentfolder/mysite/apache/django.wsgi <Directory /path/to/developmentfolder/mysite/apache> Order allow,deny Allow from all </Directory> </VirtualHost>
請注意,我根本沒有為此設置更改 httpd.conf(我不知道這是否是一個錯誤)。
然後我修改了ports.conf:
#NameVirtualHost *:80 #Listen 80 #Modificaciones para usar nginx Listen 8080 NameVirtualHost *:8080
接下來,我創建了
import os, sys apache_configuration = os.path.dirname(__file__) project = os.path.dirname(apache_configuration) workspace = os.path.dirname(project) sys.path.append(workspace) sys.path.append('/path/to/developmentfolder/mysite') os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
然後執行:
# a2ensite mysite #/etc/init.d/apache2 restart
我已經完成了 Apache 配置。
此時,在我的 hosts 文件中添加一個條目後,我可以轉到 mysite.dev:8080 並查看來自 Django 的“It works”消息。
Nginx
我安裝了 Nginx 1.6。
創建了一個使用者來執行 nginx 伺服器:
# adduser –system –no-create-home –disabled-login –disabled-password –group nginx
然後為nginx自動啟動創建了一個腳本,我實際上是從http://library.linode.com/assets/658-init-deb.sh下載的,放在/etc/init.d/,重命名為nginx然後做了一些改動:
#! /bin/sh ### BEGIN INIT INFO # Provides: nginx # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts the nginx web server # Description: starts nginx using start-stop-daemon ### END INIT INFO PATH=/opt/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin # Changed the path of the DAEMON as the one in the original script didn't match mine DAEMON=/usr/sbin/nginx #Everything from here is exactly as it was NAME=nginx DESC=nginx test -x $DAEMON || exit 0 # Include nginx defaults if available if [ -f /etc/default/nginx ] ; then . /etc/default/nginx fi set -e case "$1" in start) echo -n "Starting $DESC: " start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$NAME.pid \ --exec $DAEMON -- $DAEMON_OPTS echo "$NAME." ;; stop) echo -n "Stopping $DESC: " start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid \ --exec $DAEMON echo "$NAME." ;; restart|force-reload) echo -n "Restarting $DESC: " start-stop-daemon --stop --quiet --pidfile \ /opt/nginx/logs/$NAME.pid --exec $DAEMON sleep 1 start-stop-daemon --start --quiet --pidfile \ /opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS echo "$NAME." ;; reload) echo -n "Reloading $DESC configuration: " start-stop-daemon --stop --signal HUP --quiet --pidfile /opt/nginx/logs/$NAME.pid \ --exec $DAEMON echo "$NAME." ;; *) N=/etc/init.d/$NAME echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2 exit 1 ;; esac exit 0
接下來,創建文件夾**/usr/local/nginx/conf/sites-aviable/和/usr/local/nginx/conf/sites-enabled/**並編輯 /usr/local/nginx/conf/nginx.conf 到這個(之後對教程中的原始內容進行了一些修改):
user nginx; worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } http { #modificado el include con una ruta donde realmente se encuentra el archivo include /etc/nginx/mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; keepalive_timeout 65; gzip on; include /usr/local/nginx/conf/sites-enabled/*; }
然後,創建 /usr/local/nginx/conf/sites-aviable/mysite 。由於我的 Apache 似乎工作正常,並且在閱讀了一些 Nginx 建議之後,我覺得問題出在這個文件中,但我是伺服器新手,不知道為什麼:
server { listen 80; server_name mysite.dev; access_log /path/to/developmentfolder/mysite/logs/nginx_access.log; error_log /path/to/developmentfolder/mysite/logs/nginx_error.log; location / { proxy_pass http://mysite.dev:8080; include /usr/local/nginx/conf/proxy.conf; } location ~ /(js/|css/|imagenes/|media).* { root /path/to/developmentfolder/mysite/mysite/static; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
這是上麵包含的 proxy.conf 文件:
proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k;
然後在 sites-enabled 中創建一個符號連結到 sites-available 中的配置文件:# ln -s /usr/local/nginx/conf/sites-aviable/ceia /usr/local/nginx/conf/sites-enabled/
重啟nginx:
# /etc/init.d/nginx restart
當我訪問 mysite.dev 時,我只得到“歡迎使用 Nginx”頁面,但是,如果我理解正確,它應該是“它工作”(歡迎使用 django)頁面。
我還注意到我找不到 nginx 錯誤日誌,不在我的開發文件夾(位於我的主文件夾中)或 /usr/local/nginx/conf
如何讓 nginx 正確重定向到 apache?
這是與我創建的 nginx 使用者相關的權限問題嗎?
非常感謝,對於巨大的文章感到抱歉。
尤里卡,我成功了。
主要問題是教程資訊,它明顯過時了。
我安裝了 nginx 1.6,它創建了一個類似於 Apache 的文件夾結構,具有可用的站點和已啟用的站點。我在 /usr/local/nginx 中創建這些文件夾的步驟沒有必要(如果您不知道如何配置它也沒有用)…
預設的 conf 文件也位於安裝目錄 /etc/nginx/nginx.conf 中。我不必在這個文件中添加任何行。
這是 mysite 的 conf 文件,位於 /etc/nginx/sites-available (也從啟用站點連結)
server { listen 80; server_name mysite.dev; location / { proxy_pass http://mysite.dev:8080/; #The following lines were at proxy.conf, but I copied them here trying to avoid any # problems managing permissions or files. The include directive for proxy.conf is commented. proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; # This might work, but bypassed it to test a simpler solution # include /usr/local/nginx/conf/proxy.conf; } location ^~ /static/{ alias /var/www/mysite/static/; access_log off; } # I'm not completely sure about the following, but I don't have any troubles with it yet and # as far as I know, it might be right. error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
其余文件如我在問題中列出的那樣。
然後,不要忘記在修改後刪除您的瀏覽器記憶體,否則您可能會發瘋。
感謝大家的幫助。我希望這個答案可以幫助人們將來面臨同樣的問題。