Question

Ajax not working in LEMP

Hi. I made my site through LEMP. (ubuntu 18.04 LTS, nginx 1.17.10, mariaDB 10.3, php 7.4)

I settled the Pagespeed, Brotli, Fastcgi Cache. But I don’t know why Ajax don’t work.

configuration code is like below…

[nginx.conf]

load_module modules/ngx_pagespeed.so;
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

user  www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    ## pagespeed ##
    pagespeed on;
    pagespeed ProcessScriptVariables on;
    pagespeed FetchWithGzip on;
    pagespeed RewriteLevel CoreFilters;
    pagespeed RespectVary off;
    pagespeed DisableRewriteOnNoTransform off;
    pagespeed SupportNoScriptEnabled false;
    pagespeed ModifyCachingHeaders on;
    pagespeed ListOutstandingUrlsOnError on;
    pagespeed MaxCacheableContentLength -1;
    pagespeed FileCachePath /var/ngx_pagespeed_cache;
    pagespeed MessageBufferSize 100000;
    pagespeed XHeaderValue "Powered By Pagespeed";
    pagespeed FileCacheSizeKb 102400;
    pagespeed FileCacheCleanIntervalMs 3600000;
    pagespeed FileCacheInodeLimit 500000;
    pagespeed RewriteLevel PassThrough;
    pagespeed EnableFilters canonicalize_javascript_libraries,extend_cache,extend_cache_pdfs,combine_css,combine_javascript,move_css_above_scripts,insert_dns_prefetch,rewrite_javascript,rewrite_images,prioritize_critical_css,rewrite_css,rewrite_style_attributes,convert_meta_tags,lazyload_images,collapse_whitespace,move_css_to_head,remove_comments,remove_quotes,defer_javascript,inline_css,inline_javascript;
    pagespeed UseNativeFetcher on;
    resolver 8.8.8.8;
    pagespeed LRUCacheKbPerProcess     2048;
    pagespeed LRUCacheByteLimit        8192;
    pagespeed CreateSharedMemoryMetadataCache "/dev/shm/ngx_shared" 6400;
    pagespeed CssInlineMaxBytes 1024;
    pagespeed JsInlineMaxBytes 1024;
    pagespeed RedisServer "localhost:6379";
    pagespeed RedisTimeoutUs 1000;
    pagespeed UsePerVhostStatistics on;
    pagespeed Statistics on;
    pagespeed StatisticsLogging on;
    pagespeed StatisticsLoggingIntervalMs 60000;
    pagespeed StatisticsLoggingMaxFileSizeKb 1024;
    pagespeed MessageBufferSize 100000;
    pagespeed LogDir /var/log/pagespeed;
    pagespeed StatisticsPath /ngx_pagespeed_statistics;
    pagespeed GlobalStatisticsPath /ngx_pagespeed_global_statistics;
    pagespeed MessagesPath /ngx_pagespeed_message;
    pagespeed ConsolePath /pagespeed_console;
    pagespeed AdminPath /pagespeed_admin;
    pagespeed GlobalAdminPath /pagespeed_global_admin;
    pagespeed EnableCachePurge on;
    pagespeed PurgeMethod PURGE;
    pagespeed DownstreamCacheRewrittenPercentageThreshold 95;

    ## Fastcgi Cache ##
    client_max_body_size 64m;
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;

    ## gzip ##
    gzip on;
    gzip_http_version 1.1;
    gzip_disable "msie6";
    gzip_static on;
    gzip_vary on;
    gzip_min_length 0;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_types   text/plain text/css text/javascript text/xml
             application/javascript application/json application/x-javascript
             application/xml application/xml+rss
             image/svg+xml image/svg;
}

[default.conf] in “conf.d” directory

fastcgi_cache_path /var/run/nginx-fastcgi-cache levels=1:2 keys_zone=FASTCGICACHE:100m inactive=30s;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;



server {
    listen 80;
    server_name mysite.com;

    location / {
      return 301 https://mysite.com$request_uri;
    }
}





server {
    listen 443 ssl http2;
    server_name mysite.com;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;


    include /etc/nginx/snippets/letsencrypt.conf;

    ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem;
    include /etc/nginx/snippets/ssl.conf;


    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;
    }

    location / {
      try_files $uri $uri/ =404;
      if (!-e $request_filename) {
        rewrite ^.*$ /index.php last;
      }
    }

    location ~ \.php$ {
      #include fastcgi.conf;
      fastcgi_pass unix:/run/php/php7.4-fpm.sock;
      fastcgi_cache_bypass $skip_cache;
      fastcgi_no_cache $skip_cache;
      fastcgi_cache FASTCGICACHE;
      fastcgi_cache_valid 30s;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
      add_header X-FastCGI-Cache $upstream_cache_status;
    }

    location ~ /\.ht {
        deny all;
    }


    ## Browser Cache ##
    location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
      access_log        off;
      log_not_found     off;
      expires           30s;
    }

    ## Pagespeed ##
    location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {add_header "" ""; }
    location ~ "^/pagespeed_static/" { }
    location ~ "^/ngx_pagespeed_beacon$" { }
    pagespeed RespectVary on;
    pagespeed FileCacheSizeKb 102400;
    pagespeed FileCacheCleanIntervalMs 3600000;
    pagespeed FileCacheInodeLimit 500000;
    pagespeed LRUCacheKbPerProcess 8192;
    pagespeed LRUCacheByteLimit 16384;
    pagespeed RewriteLevel CoreFilters;
      pagespeed FetchHttps enable,allow_self_signed;
      pagespeed MaxCacheableContentLength -1;
      pagespeed EnableFilters add_instrumentation;
      pagespeed EnableFilters insert_dns_prefetch;
      pagespeed EnableFilters extend_cache;
      pagespeed EnableFilters extend_cache_pdfs;
      pagespeed EnableFilters local_storage_cache;
      pagespeed EnableFilters combine_heads;
      pagespeed EnableFilters collapse_whitespace;
      pagespeed EnableFilters convert_meta_tags;
      pagespeed EnableFilters elide_attributes;
      pagespeed EnableFilters pedantic;
      pagespeed EnableFilters remove_comments;
      pagespeed EnableFilters remove_quotes;
      pagespeed EnableFilters trim_urls;
      pagespeed EnableFilters combine_javascript;
      pagespeed EnableFilters canonicalize_javascript_libraries;
      pagespeed EnableFilters inline_javascript;
      pagespeed EnableFilters outline_css;
      pagespeed EnableFilters combine_css;
      pagespeed EnableFilters inline_import_to_link;
      pagespeed EnableFilters inline_css;
      pagespeed EnableFilters inline_google_font_css;
      pagespeed EnableFilters move_css_above_scripts;
      pagespeed EnableFilters move_css_to_head;
      pagespeed EnableFilters prioritize_critical_css;
      pagespeed EnableFilters rewrite_css;
      pagespeed EnableFilters fallback_rewrite_css_urls;
      pagespeed EnableFilters rewrite_style_attributes_with_url;
      pagespeed EnableFilters dedup_inlined_images;
      pagespeed EnableFilters inline_preview_images;
      pagespeed EnableFilters resize_mobile_images;
      pagespeed EnableFilters lazyload_images;
      pagespeed EnableFilters inline_images;
      pagespeed EnableFilters convert_gif_to_png;
      pagespeed EnableFilters convert_jpeg_to_progressive;
      pagespeed EnableFilters recompress_jpeg;
      pagespeed EnableFilters recompress_png;
      pagespeed EnableFilters recompress_webp;
      pagespeed EnableFilters strip_image_color_profile;
      pagespeed EnableFilters strip_image_meta_data;
      pagespeed EnableFilters jpeg_subsampling;
      pagespeed EnableFilters convert_png_to_jpeg;
      pagespeed EnableFilters resize_images;
      pagespeed EnableFilters resize_rendered_image_dimensions;
      pagespeed EnableFilters convert_jpeg_to_webp;
      pagespeed EnableFilters convert_to_webp_lossless;
      pagespeed EnableFilters insert_image_dimensions;
      pagespeed NoTransformOptimizedImages on;
      pagespeed EnableFilters sprite_images;



    ## Brotli ##
    # Dynamic mode.
    brotli on;
    brotli_comp_level 6;
    brotli_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/vnd.microsoft.icon image/bmp image/svg+xml;
    # Static mode.
    brotli_static on;
}

Please help me :)


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi there @joshuarang,

The Nginx configuration looks correct, what is the exact error that you get for your AJAX requests? Can you share the error from your web console here?

Also, I would recommend having a look at your Nginx error log for more information about the actual error:

  1. sudo tail -100 /var/log/nginx/error.log

Another thing that you could take a look at is this answer here on how to troubleshoot common Nginx problems:

https://www.digitalocean.com/community/questions/how-to-troubleshoot-common-nginx-issues-on-linux-server

Regards, Bobby