Nginx + Apache mod_wsgi,麻烦了解如何configuration服务器

我一直试图在Ubuntu 12.04上设置一个开发环境太久。 我的意图是在后面的生产环境中设置一切,所以我按照这个教程来学习如何将Nginx设置为Apache 2的反向代理。

我一直在寻找,我的问题与这个问题非常相似,但没有给出答案,我不能评论它来增加我的一些研究。

让我们进入文件:

Apache和wsgi

首先,我从apt-get安装了mod_wsgi,然后在/ etc / apache2 / sites-available中创build了一个mysite文件:

<VirtualHost *:8080> ServerAdmin [email protected] 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 

接下来,我创build了

 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的configuration。

在这一点上,添加一个条目到我的主机文件后,我可以去mysite.dev:8080,看看Django的“It works”消息。

Nginx的

我安装了Nginx 1.6。

创build一个用户来运行nginx服务器:

  # adduser –system –no-create-home –disabled-login –disabled-password –group nginx 

然后为nginx创build一个脚本自动启动,我从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 

接下来,创build文件夹/ 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/*; } 

然后,创build/ 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; 

然后在site-enabled中创build一个符号链接到configuration文件中可用:#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错误日志,不在我的开发文件夹(位于我的home文件夹)或在/ usr / local / nginx / conf

我怎样才能让nginxredirect到Apache?

这是一个与我创build的nginx用户相关的权限问题吗?

非常感谢,抱歉的巨大的职位。

尤里卡,我做到了。

主要问题是教程信息,它显着过时了。

我安装了nginx 1.6,它创build了一个与Apache类似的文件夹结构,其中包括可用的站点和启用了站点的站点。 在我在/ usr / local / nginx中创build这些文件夹的步骤中没有必要(也没有用,如果你不知道如何configuration它)…

默认的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; } } 

其余的文件是我在问题中列出的。

然后,不要忘记修改后删除浏览器caching或者你可以发疯。

感谢大家的帮助。 我希望这个答案有助于将来面对同样问题的人们。

closures蝙蝠,您在Apache的虚拟主机定义中有两个结束标记。 我假定你已经保留了Apache的默认服务器文档根目录,即使它没有列出。

对于你的NGINXconfiguration,我注意到你还没有列出proxy_redirect 。 尝试在你的NGINX服务器configuration中包括这个,看看是否手动指定位置标题帮助。

另外,这里是我在创build自己的开发环境时引用的一个很好的教程 。 (对于静态文件使用NGINX,对于dynamic使用Apapche)。 也许它会给你更多的启发和正确运行的见解。