我有一个 nginx.conf 文件,如下所示:
http {
server {
listen 80;
server_name myapp.org;
return 301 http://www.myapp.org$request_uri;
}
server {
listen 80;
server_name www.myapp.org;
passenger_enabled on;
root /var/www/myapp/current/public;
}
}
这看起来像是该网站和其他网站推荐的格式,但是当我访问 myapp.org 时,我只从 nginx 收到此消息:
欢迎使用 nginx!
如果您看到此页面,则表示 nginx Web 服务器已成功安装并正常运行。需要进一步配置。
有关在线文档和支持,请参阅 nginx.org。商业支持可在 nginx.com 上获得。
感谢您使用 nginx。
但是如果我访问 www.myapp.org,那么一切都会按预期运行。为什么重定向没有被接受?
更新:
nginx.conf:
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# passenger_friendly_error_pages on;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
# include /etc/nginx/naxsi_core.rules;
##
# Phusion Passenger config
##
# Uncomment it if you installed passenger or passenger-enterprise
##
passenger_root /home/deploy/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/passenger-5.0.4;
passenger_ruby /home/deploy/.rbenv/versions/2.0.0-p247/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80;
server_name coolthearth.net;
return 301 http://www.cooltheearth.org$request_uri;
}
server {
listen 80;
server_name www.coolthearth.net;
return 301 http://www.cooltheearth.org$request_uri;
}
server {
listen 80;
server_name coolthearth.org;
return 301 http://www.cooltheearth.org$request_uri;
}
server {
listen 80;
server_name www.cooltheearth.org;
passenger_enabled on;
root /var/www/rails_backend/current/public;
}
}
# mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
# }
/etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/www;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
}
答案1
您的server_name
指令中有拼写错误。
您在 3 处写的coolthearth
是 而不是。cooltheearth
测试当前设置并输入错误,结果如下:
curl -H "Host: coolthearth.org" "http://104.131.135.243" -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.2
Date: Fri, 20 Mar 2015 22:22:51 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://www.cooltheearth.org/
修复这些问题它就可以正常工作。