Nginx 配置中的多个问题

Nginx 配置中的多个问题

我对 Nginx 还很陌生,我无法让它工作。我有一个托管在 Azure 中的 Web 应用程序。这是一个 WordPress 网站。

这是 Nginx 文件:

server {
    #proxy_cache cache;
        #proxy_cache_valid 200 1s;

    listen 8080;
    listen [::]:8080;
    root /home/site/wwwroot;
    index  index.php;

    server_name  my-app.azurewebsites.net; 
    port_in_redirect off;

    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    # Disable sendfile as per https://docs.vagrantup.com/v2/synced-folders/virtualbox.html
        sendfile off;
        set $skip_cache 0;

        # POST requests and urls with a query string should always go to PHP
        if ($request_method = POST) {
                set $skip_cache 1;
        }

        if ($query_string != "") {
                set $skip_cache 1;
        }

        # Don't cache uris containing the following segments
        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
                set $skip_cache 1;
        }

        # Don't use the cache for logged in users or recent commenters
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
                set $skip_cache 1;
        }

        # Don't cache WooCommerce URLs
        # Cart widgets are still a problem: https://github.com/emcniece/docker-wordpress/issues/3
        if ($request_uri ~* "/(cart|checkout|my-account)/*$") {
                set $skip_cache 1;
        }

    location / {            
        try_files $uri $uri/ /index.php?$args;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /html/;
    }
    
    # Disable .git directory
    location ~ /\.git {
        deny all;
        access_log off;
        log_not_found off;
    }

    # Add locations of phpmyadmin here.
#    location ~ [^/]\.php(/|$) {
    location ~ \.php$ {
#        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        fastcgi_pass 127.0.0.1:9000;
#       fastcgi_pass php;

        include fastcgi_params;

        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;

        fastcgi_intercept_errors on;
#        fastcgi_connect_timeout         300; 
#        fastcgi_send_timeout           3600; 
#        fastcgi_read_timeout           3600;
#        fastcgi_buffer_size 128k;
#        fastcgi_buffers 4 256k;
#        fastcgi_busy_buffers_size 256k;
#        fastcgi_temp_file_write_size 256k;

        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        fastcgi_cache off;
        fastcgi_cache_valid 60m;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }

    add_header "Access-Control-Allow-Origin"  *;
}

server {
    listen 80;
    server_name my-app.azurewebsites.net;
    return 301 https://$host$request_uri;
}
  1. 我的第一个问题与标头有关。我尝试添加一些标头,但它们被完全忽略了。我在主 Nginx 文件夹中搜索了父块,希望找到覆盖它的内容,但没有找到与标头相关的任何内容。

  2. 另一个问题与我无法访问的 /wp-admin 文件夹有关。它进入某个循环,响应代码为 302。

  3. 我不知道为什么,但有些资源是通过http以及其他一些https

任何建议都将不胜感激。

提前致谢,Ciprian

答案1

这里最大的问题是您的配置文件中充满了剪切和粘贴的指令,这使得调试变得不可能。例如,为什么要禁用 sendfile?您似乎有很多用于绕过本地缓存的配置,但没有定义本地缓存。请学习如何使用指令include使其更易于管理并摆脱冗余内容。

我尝试添加一些标题,但它们被完全忽略了

您没有告诉我们您尝试了什么。IME ngx_http_headers_module 是大多数 nginx 发行版的标准配置,并且 add_header 的工作原理与预期完全一致。

另一个问题与我无法访问的 /wp-admin 文件夹有关。它进入某个循环,响应代码为 302。

可能重定向到您刚刚请求的 URL?(如果您提供了请求的 URL 和响应标头,那会很有帮助)。这是因为 wp_admin 检查传入连接是否通过 https。您的不是。您的 SSL 似乎在反向代理上终止(再次,我的水晶球加班了)。快速谷歌搜索后,结果显示https://wordpress.stackexchange.com/questions/387990/how-do-i-handle-ssl-properly-when-wp-is-behind-a-reverse-proxyhttp://www.michaelfoster82.co.uk/how-to-set-up-wordpress-behind-a-secure-reverse-proxy-using-nginx/

我不知道为什么,但有些资源是通过 http 加载的,而另一些资源是通过 https 加载的

编程很马虎。但让 wordpress 相信传入连接是通过 HTTPS(见上文)应该可以解决。

相关内容