【问题标题】:Nginx Default: Why is location / not forwarded to reverse proxy?Nginx 默认:为什么位置/不转发到反向代理?
【发布时间】:2017-07-26 11:58:34
【问题描述】:

我希望 NGINX 将任何对域名http://home.com 的请求转发到proxy_pass http://localhost:8866;。我在ownCloud的文档根目录所在的NGINX配置文件中设置了一个根root /home/owncloud;。这应该指向http://home.com/owncloudhttp://localhost:8866 下我有一个运行托管Wordpress 的Docker 容器。但是,当我尝试访问 http://home.com 时,NGINX 总是将反向代理指向服务器的实际文档根目录 (root /home/owncloud)

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

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

【问题讨论】:

    标签: wordpress nginx proxy


    【解决方案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 / {} 表示“/ 的完全匹配”。 在您的代码中,没有真正尝试将 all 请求转发到 localhost。仅转发对索引页面(文档根,即“/”)的请求。

    如果您将 所有 请求传递给 localhost:8866 的进程,则甚至不会测试任何其他 location 部分,因此一旦您确保您的 proxy_pass 有效,您可以删除他们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      • 2021-06-28
      • 1970-01-01
      • 2019-03-14
      • 2019-07-28
      • 2020-12-18
      • 1970-01-01
      相关资源
      最近更新 更多