2017-03-06 70 views
0

我希望NGINX將任何請求轉發到域名http://home.comproxy_pass http://localhost:8866;。我在ownCloud文檔根所在的NGINX配置文件中設置了一個根root /home/owncloud;。這應該指向http://home.com/owncloudhttp://localhost:8866下我有一個運行託管Wordpress的Docker容器。然而,NGINX總是指向反向代理服務器(root /home/owncloud),當我嘗試訪問http://home.comNginx默認:爲什麼位置/不被轉發到反向代理?

感謝您輸入這個實際文檔根 - 我已經經歷了很多文檔的瀏覽,但是目前我無法自己找到解決方案。謝謝!

這是我NGINX配置文件:

upstream php-handler { 
    server unix:/var/run/php5-fpm.sock; 
    } 

server { 
    listen 80; 
    server_name home.com; 
    index index.html index.htm index.php; 
    # enforce https 
    return 301 https://$server_name$request_uri; 
    } 


server { 
     ssl on; 
     listen 443 ssl; 
     server_name home.com; 
     server_name 123.456.789.10 ssl; 
     ssl_certificate /home/ssl/certificate.pem; 
     ssl_certificate_key /home/ssl/owncloud.key; 
     index index.html index.htm index.php; 

     root /space/owncloud; 
     try_files $uri $uri/ /index.php?q=$request_uri; 
     # Add headers to serve security related headers 
       add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;"; 
       add_header X-Content-Type-Options nosniff; 
       add_header X-Frame-Options "SAMEORIGIN"; 
       add_header X-XSS-Protection "1; mode=block"; 
       add_header X-Robots-Tag none; 

     # set max upload size 
     client_header_buffer_size 64k; 
     large_client_header_buffers 4 64k; 

     # Disable gzip to avoid the removal of the ETag header 
     gzip off; 

     rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect; 
     rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect; 
     rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect; 

     index index.php index.html index.htm; 
     error_page 403 /core/templates/403.php; 
     error_page 404 /core/templates/404.php; 

    location =/{ 
       proxy_set_header  Host $host; 
       proxy_set_header  X-Real-IP $remote_addr; 
       proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for; 
       proxy_set_header  X-Forwarded-Proto $scheme; 
       proxy_pass   http://localhost:8866; 
       proxy_read_timeout 90; 
     } 

    location ~ ^/(?:\.htaccess|data|config|db_structure\.xml|README){ 
    deny all; 
    } 

    location = /favicon.ico { log_not_found off; access_log off; } 
    location = /robots.txt { log_not_found off; access_log off; allow all; } 
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ { 
     expires max; 
     log_not_found off; 
     } 

     location ~ \.php$ { 
      fastcgi_split_path_info ^(.+\.php)(/.+)$; 
      fastcgi_pass unix:/var/run/php5-fpm.sock; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      fastcgi_index index.php; 
      include fastcgi_params; 
      proxy_pass_header Authorization; 
      proxy_set_header Host $host; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_http_version 1.1; 
      proxy_set_header Connection ""; 
      proxy_buffering off; 
    }  


      location /owncloud { 
      index index.html index.htm index.php; 

      rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect; 
      rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect; 
      rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect; 

      error_page 403 /core/templates/403.php; 
      error_page 404 /core/templates/404.php; 

      location ~ \.php(?:$|/) { 
       fastcgi_split_path_info ^(.+\.php)(/.+)$; 
       include fastcgi_params 
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
       fastcgi_param PATH_INFO $fastcgi_path_info; 
       fastcgi_param HTTPS on; 
       fastcgi_param modHeadersAvailable true; #Avoid sending the security headers twice 
       fastcgi_pass php-handler; 
       fastcgi_intercept_errors on; 
       fastcgi_buffer_size 128k; 
       fastcgi_buffers 4 256k; 
       fastcgi_busy_buffers_size 256k; 
       # attachments can be huge 
       client_max_body_size 513M; 
       client_body_in_file_only clean; 
       # this is where requests body are saved 
       client_body_temp_path /opt/nginx/bugzilla/data/request_body 1 2; 
     } 
    } 

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

回答

0

如果你需要將所有請求在您的本地主機的另一個端口,您只需要用這樣的:

location/{ 
    proxy_set_header  Host $host; 
    proxy_set_header  X-Real-IP $remote_addr; 
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header  X-Forwarded-Proto $scheme; 
    proxy_pass    http://localhost:8866$request_uri; 
    proxy_read_timeout 90; 
} 

注意,沒有「= 「斜槓前的等號/location/{}的意思是「完全匹配/」。 在你的代碼中沒有真正試圖將全部請求轉發到本地主機。僅索引頁面(文檔根,即「/」)的請求被轉發。

如果您通過所有請求的處理在本地主機:8866,讓利location部分甚至會受到考驗,所以一旦你確保你的proxy_pass的作品,你可以將它們刪除。