2015-04-04 99 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