Nginx默认值:为什么位置/不转发到反向代理?

时间:2017-03-06 14:50:35

标签: wordpress nginx proxy

我希望NGINX将对域名http://home.com的任何请求转发给proxy_pass http://localhost:8866;。我在NGINX配置文件中设置了一个根root /home/owncloud;,其中ownCloud的文档根位于该文件中。这应该指向http://home.com/owncloudhttp://localhost:8866下我有一个运行托管Wordpress的Docker容器。但是,当我尝试访问root /home/owncloud

时,NGINX始终将反向代理指向服务器的实际文档根目录http://home.com

感谢您对此的意见 - 我已浏览了大量文档,但目前我无法找到解决方案。谢谢!

这是我的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;
            }
}

1 个答案:

答案 0 :(得分:1)

如果您需要将所有请求转发到本地主机的另一个端口,则只需要这样:

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 / {}表示“与/完全匹配”。 在您的代码中,没有什么可以尝试将所有请求转发给localhost。只转发对索引页面(文档根目录,即“/”)的请求。

如果您将所有请求传递给localhost:8866上的进程,则其他任何location部分都不会被测试,因此一旦确定proxy_pass工作正常,你可以删除它们。