我一直在尝试在 Ubuntu 12.04 上设置开发环境。我的目的是设置好一切,就像我稍后在生产环境中必须做的那样,所以我遵循了本教程关于如何使用 Apache 2 设置 Nginx 作为反向代理。
我一直在搜索,我的问题与这个,但没有给出答案,我无法对其发表评论以添加我的一些研究。
让我们进入文件:
Apache 和 wsgi
首先,我从 apt-get 安装了 mod_wsgi,然后在 /etc/apache2/sites-available 中创建了一个 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
接下来我创建
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 的“它有效”消息。
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 用户相关的权限问题吗?
非常感谢,抱歉写了这么长的帖子。
答案1
尤里卡,我成功了。
主要问题在于教程信息,它已经严重过时了。
我安装了 nginx 1.6,它创建了一个类似于 Apache 的文件夹结构,其中包含 sites-available 和 sites-enabled。我创建这些文件夹的步骤在 /usr/local/nginx 中,没有必要(如果您不知道如何配置它,也没有用)...
默认的conf文件也位于安装目录中, /etc/nginx/nginx.conf 。我不需要在这个文件中添加任何行。
这是 mysite 的 conf 文件,位于 /etc/nginx/sites-available(也链接到 sites-enabled)
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;
}
}
其余文件正如我在问题中列出的一样。
然后,修改后不要忘记删除浏览器缓存,否则你可能会发疯。
谢谢大家的帮助。希望这个答案能帮助将来遇到同样问题的人。