从 Apache 迁移到 Nginx 后,我们的应用程序前端无法正常工作

从 Apache 迁移到 Nginx 后,我们的应用程序前端无法正常工作

这是我们的配置文件,我们在 Apache 上运行的应用程序的前端功能方面遇到了问题。

Javascript POST 方法不再起作用。

server {

    listen   80;
    server_name website.com www.website.com;
    rewrite ^/(.*) https://website.com/$1 permanent;
}

server {

    listen   443;
    server_name website.com www.website.com;


    ssl    on;
    ssl_certificate    /etc/ssl/www_website_com.pem;
    ssl_certificate_key    /etc/ssl/website.com.key;

    ssl_session_timeout 5m;

    root        /usr/share/nginx/www.website.com;
    index index.html index.php;

    # canonicalize codeigniter url end points
    # if your default controller is something other than "welcome" you should change the following
    if ($request_uri ~* ^(/home(/index)?|/index(.php)?)/?$)
    {
            rewrite ^(.*)$ / permanent;
    }

    # removes trailing "index" from all controllers
    if ($request_uri ~* index/?$)
    {
            rewrite ^/(.*)/index/?$ /$1 permanent;
    }

    # removes trailing slashes (prevents SEO duplicate content issues)
    if (!-d $request_filename)
    {
            rewrite ^/(.+)/$ /$1 permanent;
    }

    # removes access to "system" folder, also allows a "System.php" controller
    if ($request_uri ~* ^/system)
    {
            rewrite ^/(.*)$ /index.php?/$1 last;
            break;
    }

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    if (!-e $request_filename)
    {
            rewrite ^/(.*)$ /index.php?/$1 last;
          break;
    }

    # catch all
    error_page 404 /index.php;

    # set expiration of assets to MAX for caching
    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
            expires max;
            log_not_found off;
    }

    location / {
            # Check if a file exists, or route it to index.php.
            try_files $uri $uri/ /index.php;
            auth_basic            "Restricted";
            auth_basic_user_file  /etc/nginx/htpasswd;
    }

    location ~* \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_connect_timeout 60;
            fastcgi_send_timeout 180;
            fastcgi_read_timeout 180;
            fastcgi_buffers 512 32k;
            fastcgi_buffer_size 64k;
            include fastcgi_params;

    # PHP only, required if PHP was built with --enable-force-cgi-redirect
            fastcgi_param   REDIRECT_STATUS         200;
            fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www.website.com$fastcgi_script_name;
    }

    location ~ /\.ht
{
    deny all;
  }

  }

回答:

我终于知道哪里出了问题以及如何修复它。nginx 的配置应该仅适用于静态 JS,而不适用于 PHP 操作。

配置如下:

location ~ ^/(assets)/ {
  expires max;
  break;
 }

将“assets”替换为“js”它就会起作用。

答案1

您的(永久)重写规则将为每个请求(包括 POST)返回 301 重定向。因此,任何帖子变量都将丢失

答案2

回答:

我终于知道哪里出了问题以及如何修复它。nginx 的配置应该仅适用于静态 JS,而不适用于 PHP 操作。

配置如下:

location ~ ^/(assets)/ {
 expires max;
 break;
}

将“assets”替换为“js”它就会起作用。

相关内容