NGINX adding code for browser cachine giving error into file nginx.conf

OK, so there is what i did to my nginx.conf file:

#user nginx;
worker_processes 1;

#error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;

#pid /var/run/nginx.pid;

include /etc/nginx/modules.conf.d/*.conf;

events {
worker_connections 1024;
}

location ~* \.(txt|xml|js)$ {
expires 8d;
}

location ~* \.(css)$ {
expires 8d;
}

location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$ {
expires 8d;
}

location ~* \.(jpg|jpeg|png|gif|swf|webp)$ {
expires 8d;
}

http {
include 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 0;
keepalive_timeout 65;
#tcp_nodelay on;

#gzip on;
#gzip_disable "MSIE [1-6]\.(?!.*SV1)";

server_tokens off;

include /etc/nginx/conf.d/*.conf;
}

# override global parameters e.g. worker_rlimit_nofile
include /etc/nginx/*global_params;

and here is the error i get when i try to reload nginx:
[root@u18879177 ~]# service nginx reload
Reloading nginx configuration (via systemctl): Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[FAILED]

what am i doign wrong?

thank you

  • Nithin

    Hi M.,

    Hope you are doing good today. :slight_smile:

    I'm not a NGINX expert, but could you please share your 'journalctl -xe' log so that we could give a closer look on where it throws an error related to this, to get a clear idea about it, please run: systemctl status nginx.service

    Please let us know how that goes, so that we could help sort this out faster. :slight_smile:

    Kind Regards,
    Nithin

  • M.

    Hi thanks for your help, here is the output:

    [root@u18879177 ~]# systemctl status nginx.service
    ? nginx.service - Startup script for nginx service
    Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
    Active: active (running) (Result: exit-code) since Sun 2016-05-08 03:24:31 EDT; 5 days ago
    Process: 20176 ExecReload=/bin/kill -s HUP $MAINPID (code=exited, status=0/SUCCESS)
    Process: 20174 ExecReload=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
    Process: 20172 ExecReload=/usr/bin/test $NGINX_ENABLED = yes (code=exited, status=0/SUCCESS)
    Main PID: 8893 (nginx)
    CGroup: /system.slice/nginx.service
    ?? 8893 nginx: master process /usr/sbin/nginx
    ??20178 nginx: worker process

    May 12 20:32:15 u18879177.onlinehome-server.com nginx[20023]: nginx: configuration file /etc/nginx/nginx.conf test failed
    May 12 20:32:15 u18879177.onlinehome-server.com systemd[1]: nginx.service: control process exited, code=exited status=1
    May 12 20:32:15 u18879177.onlinehome-server.com systemd[1]: Reload failed for Startup script for nginx service.
    May 12 20:35:20 u18879177.onlinehome-server.com nginx[20139]: nginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:17
    May 12 20:35:20 u18879177.onlinehome-server.com nginx[20139]: nginx: configuration file /etc/nginx/nginx.conf test failed
    May 12 20:35:20 u18879177.onlinehome-server.com systemd[1]: nginx.service: control process exited, code=exited status=1
    May 12 20:35:20 u18879177.onlinehome-server.com systemd[1]: Reload failed for Startup script for nginx service.
    May 12 20:35:33 u18879177.onlinehome-server.com nginx[20174]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    May 12 20:35:33 u18879177.onlinehome-server.com nginx[20174]: nginx: configuration file /etc/nginx/nginx.conf test is successful
    May 12 20:35:33 u18879177.onlinehome-server.com systemd[1]: Reloaded Startup script for nginx service.
    [root@u18879177 ~]#

  • Kasia Swiderska

    Hello M.

    It looks like you pasted location in the wrong place in config file. https://www.nginx.com/resources/admin-guide/nginx-web-server/

    These blocks are defined using the location directive placed within a server directive.

    So it should be looking like that (very short version)

    server {
    
    location {
    //rest of the code
    } 
    
    }

    Let me know if you still need any further assistance here

    kind regards,
    Kasia

  • M.

    still getting error: this is my files now:

    #user nginx;
    worker_processes 1;

    #error_log /var/log/nginx/error.log;
    #error_log /var/log/nginx/error.log notice;
    #error_log /var/log/nginx/error.log info;

    #pid /var/run/nginx.pid;

    include /etc/nginx/modules.conf.d/*.conf;

    events {
    worker_connections 1024;
    }

    server {

    location ~* \.(txt|xml|js)$ {
    expires 8d;
    }

    location ~* \.(css)$ {
    expires 8d;
    }

    location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$ {
    expires 8d;
    }

    location ~* \.(jpg|jpeg|png|gif|swf|webp)$ {
    expires 8d;
    }

    }

    http {
    include 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 0;
    keepalive_timeout 65;
    #tcp_nodelay on;

    #gzip on;
    #gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    server_tokens off;

    include /etc/nginx/conf.d/*.conf;
    }

    # override global parameters e.g. worker_rlimit_nofile
    include /etc/nginx/*global_params;

    what am i missing?

  • Kasia Swiderska

    Hello M.

    I'm terribly sorry for delay on our end! So it looks like server is in wrong place - I've had to take a closer look on the sample config file here https://www.nginx.com/resources/wiki/start/topics/examples/full/ - and I'm sorry I missed that one.
    Except events {} server has to be inside the http {} - so copy server (and everything that is inside) and move it to the http{} like in the sample config above.
    Let me know if this will finally work.

    kind regards,
    Kasia

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.