2015-04-04 98 views
1

我是nginx新手,我有一个虚拟主机的问题。当我尝试访问虚拟主机时,虚拟主机不工作,它将重定向到本地主机“欢迎使用nginx”。下面是我的配置的内容:Nginx的虚拟主机不能正常工作(错误的重定向)

/etc/hosts中配置:

127.0.0.1 localhost localhost.localdomain 

::1 localhost localhost.localdomain 

****Generated by Admin**** 

18.200.10.50 mail.testingweb.com 

18.200.10.50 testingweb.com 

SSL配置上/etc/nginx/conf.d/ssl.conf:

server { 
     listen 443 default_server ssl; 
     server_name testingweb.com; 

     ssl_certificate /etc/nginx/sslcert/xxxx.crt; 
     ssl_certificate_key /etc/nginx/sslcert/xxxxx.key; 

     ssl_session_cache shared:SSL:10m; 
     ssl_session_timeout 10m; 
     keepalive_timeout 70; 

     ssl_prefer_server_ciphers on; 
     ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNU$ 
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 

     location/{ 
       root /usr/share/nginx/html; 
       index index.php index.html index.htm; 
     } 

     location ~ \.php$ { 
       try_files $uri =404; 

       # With php5-fpm: 
       fastcgi_pass unix:/var/run/php5-fpm.sock; 
       fastcgi_index index.php; 
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
       include fastcgi_params; 
     } 

} 

/etc/nginx/sites-available/default config:

server { 
     listen 80 default_server; 
     # listen [::]:80 default_server ipv6only=on; 

     root /usr/share/nginx/xhtml; 
     index index.php index.html index.htm; 


     # Make site accessible from http://localhost/ 
     server_name testingweb.com; 
     return 301 https://$host$request_uri; 

     location/{ 
       try_files $uri $uri/ =404; 
     } 
     error_page 404 /404.html; 

     # redirect server error pages to the static page /50x.html 

     error_page 500 502 503 504 /50x.html; 
     location = /50x.html { 
       root /usr/share/nginx/html; 
     } 

     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
     # 
     location ~ \.php$ { 
       try_files $uri =404; 

     #  # With php5-fpm: 
       fastcgi_pass unix:/var/run/php5-fpm.sock; 
       fastcgi_index index.php; 
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
       include fastcgi_params; 
     } 


     # deny access to .htaccess files, if Apache's document root 
     # concurs with nginx's one 
     # 
     location ~ /\.ht { 
       deny all; 
     } 
} 
server { 
     listen 80; 
     listen 443; 
     return 403; 
} 

我想从新的根目录访问另一个网站,/usr/share/nginx/html/www在www目录上有一个wordpress

的/ etc/nginx的/网站可用/ testingweb配置:

server { 
     listen 80 default_server; 
     # listen [::]:80 default_server ipv6only=on; 

     root /usr/share/nginx/html/www; 
     index index.php index.html index.htm; 


     # Make site accessible from http://localhost/ 
     server_name testingweb.com; 

#  rewrite^https://$http_host$request_uri? permanent; 
     return 301 https://$host$request_uri; 

     location/{ 
       # First attempt to serve request as file, then 
       # as directory, then fall back to displaying a 404. 
       try_files $uri $uri/ /index.php?q=$uri&$args; 
       # Uncomment to enable naxsi on this location 
       # include /etc/nginx/naxsi.rules; 
     } 


    error_page 404 /404.html; 

    # redirect server error pages to the static page /50x.html 
    # 
    error_page 500 502 503 504 /50x.html; 
    location = /50x.html { 
      root /usr/share/nginx/html; 
    } 

     location ~ \.php$ { 
       try_files $uri =404; 

     #  # With php5-fpm: 
       fastcgi_pass unix:/var/run/php5-fpm.sock; 
       fastcgi_index index.php; 
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
       include fastcgi_params; 
     } 

#  location = /favicon.ico { 
#    alias /usr/share/nginx/html/favicon.ico; 
#  } 


     # deny access to .htaccess files, if Apache's document root 
     # concurs with nginx's one 
     # 
     location ~ /\.ht { 
       deny all; 
     } 
} 

根据CONFIGS,这有什么错我的配置?我无法访问域名/usr/share/nginx/html/www上的wordpress文件testingweb.com?它总是重定向到默认主机而不是testingweb主机?

对不起我的英文不好..

+0

你的''default''和'''''''''''''配置都会在块的中间返回301个重定向,甚至在代理请求到php-fpm之前。你的nginx是否可以工作? – kchan 2015-04-04 18:47:48

+0

是的,服务器(nginx)工作正常。那么我应该删除'testingweb'上的返回301吗? – metaphor 2015-04-05 00:36:32

+0

如果您需要将请求重定向到另一个URL,则只需要返回指令 - 因此,如果您希望最终在默认服务器块中的请求转到您的WordPress设置所在的“testingweb”,请保留返回默认配置。但是如果您有规则(位置块)来处理请求,则不需要在''testingweb''中使用它。 – kchan 2015-04-05 00:44:06

回答

2

这是从引擎收录代码nginx的配置的修订版本:

server { 
     listen 80; 
     # listen [::]:80 default_server ipv6only=on; 
     # Make site accessible from http://devdev.com/ 
     server_name devdev.com; 
     return 301 https://$host$request_uri; 
} 

# HTTPS server 
# 
server { 
     listen 443 default_server ssl; 
     server_name devdev.com; 
     root /var/www; 
     index index.php index.html index.htm; 

     # uncomment to add your access log path here 
     # access_log /var/log/nginx/devdev.com.access.log main; 

     ssl_certificate /etc/ssl/ssl-unified.crt; 
     ssl_certificate_key /etc/ssl/ssl-my-private-decrypted.key; 

     ssl_session_cache shared:SSL:10m; 
     ssl_session_timeout 10m; 
     keepalive_timeout 70; 

     ssl_prefer_server_ciphers on; 
     ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS +RC4 RC4"; 
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 

     location @default { 
       rewrite ^/(.*) /index.php?uri=$request_uri last; 
     } 

     location/{ 
       try_files $uri $uri/index.php @default; 
     } 

     location ~ \.php$ { 
       try_files $uri =404; 

       # With php5-fpm: 
       fastcgi_pass unix:/var/run/php5-fpm.sock; 
       fastcgi_index index.php; 
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
       include fastcgi_params; 
     } 

} 

端口80上的第一台服务器块监听只是重定向到https://devdev.com/。这会将所有http请求重定向到https,因此您不需要任何其他处理规则。

第二个服务器模块侦听端口443,并将以.phpphp-fpm(您想仔细检查它是否在unix套接字上运行并且您的权限正确)的路径代理请求。

匹配/前缀(location /)的位置块将尝试匹配请求URI中的文件并正确处理请求。例如:

  • 如果该请求是用于/index.php和文件存在,以下块将匹配.php后缀和代理至php-fpm
  • 如果请求是针对/foo,并且没有该名称的文件匹配,那么nginx会尝试匹配/foo/index.php,然后代理到php-fpm
  • 如果仍然没有匹配,try_files将使用@default位置块,它将请求发送到顶级/index.php,请求URI作为参数。

如果你的WordPress网站位于/var/www - 顶级入口点应该是/var/www/index.php - 这个配置应该可以工作。你可能需要根据你的WordPress设置来调整配置 - 尽管这是足够通用的,它应该在没有很多改变的情况下工作。

+0

感谢您的配置,但它仍然没有工作,当我访问(http://)devdev.com它总是重定向到** index.html **从**/usr/share/nginx/html ** .. – metaphor 2015-04-07 08:15:40

+0

问题解决了。它由SSL服务器块中的'location/{..}'错误引起。非常感谢您的帮助@kchan – metaphor 2015-04-07 12:45:36