[GH-ISSUE #31] gonginx does not seem to catch syntax errors in nginx.conf #14

Closed
opened 2026-02-28 01:20:30 +03:00 by kerem · 11 comments
Owner

Originally created by @kehoecj on GitHub (Dec 12, 2023).
Original GitHub issue: https://github.com/tufanbarisyildirim/gonginx/issues/31

Originally assigned to: @tufanbarisyildirim on GitHub.

We were hoping to use gonginx in the config-file-validator tool to provide syntax validation for nginx.conf files. During some testing we noticed that gonginx will not throw an error if the syntax of the nginx.conf file is invalid. For example, in this test we intentionally did not close a server block and it parsed without error.

Is this expected behavior, a bug, or are we parsing incorrectly?

Originally created by @kehoecj on GitHub (Dec 12, 2023). Original GitHub issue: https://github.com/tufanbarisyildirim/gonginx/issues/31 Originally assigned to: @tufanbarisyildirim on GitHub. We were hoping to use gonginx in the [config-file-validator](https://github.com/Boeing/config-file-validator) tool to provide syntax validation for `nginx.conf` files. During some testing we noticed that gonginx will not throw an error if the syntax of the `nginx.conf` file is invalid. For example, in [this test](https://github.com/Boeing/config-file-validator/issues/100#issuecomment-1850715255) we intentionally did not close a server block and it parsed without error. Is this expected behavior, a bug, or are we parsing incorrectly?
kerem 2026-02-28 01:20:30 +03:00
  • closed this issue
  • added the
    bug
    v2
    labels
Author
Owner

@kehoecj commented on GitHub (Dec 14, 2023):

@tufanbarisyildirim any ideas?

<!-- gh-comment-id:1856240626 --> @kehoecj commented on GitHub (Dec 14, 2023): @tufanbarisyildirim any ideas?
Author
Owner

@tufanbarisyildirim commented on GitHub (Dec 14, 2023):

Hello @kehoecj , sorry I have missed this, this looks like an interesting bug, thanks for raising it. I will be back here around weekend.

<!-- gh-comment-id:1856253062 --> @tufanbarisyildirim commented on GitHub (Dec 14, 2023): Hello @kehoecj , sorry I have missed this, this looks like an interesting bug, thanks for raising it. I will be back here around weekend.
Author
Owner

@tufanbarisyildirim commented on GitHub (Dec 14, 2023):

Honestly, I didn't expect it to be utilized so extensively, and I absolutely enjoy the BUGS that can offer opportunities to reshape the core logic.

DEAL

<!-- gh-comment-id:1856699416 --> @tufanbarisyildirim commented on GitHub (Dec 14, 2023): Honestly, I didn't expect it to be utilized so extensively, and I absolutely enjoy the BUGS that can offer opportunities to reshape the core logic. DEAL
Author
Owner

@tufanbarisyildirim commented on GitHub (Dec 15, 2023):

Hello @kehoecj can you please try to see if that version helps you? https://github.com/tufanbarisyildirim/gonginx/tree/v2

That was a simple workaround, I will release a version for you that you can stick with and then jump in refactoring it.

<!-- gh-comment-id:1858066807 --> @tufanbarisyildirim commented on GitHub (Dec 15, 2023): Hello @kehoecj can you please try to see if that version helps you? https://github.com/tufanbarisyildirim/gonginx/tree/v2 That was a simple workaround, I will release a version for you that you can stick with and then jump in refactoring it.
Author
Owner

@onlineque commented on GitHub (Dec 21, 2023):

Hi @tufanbarisyildirim,
thanks a lot for the fix, now the missing eof in block seems to be catched properly.

But when I feed the config with some nonsense keyword like "listena" in this example:


#user http;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    server {
        listena       8000;
        # listen       somename:8080;
        # server_name  somename  alias  another.alias;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

The gonginx says it's valid. But it's not. Testing with nginx -t -c nginx-bad-2.conf gives me expected:

2023/12/21 21:50:36 [emerg] 252611#252611: unknown directive "listena" in /etc/nginx/nginx-bad-2.conf:85
nginx: configuration file /etc/nginx/nginx-bad-2.conf test failed

Is it something what can be fixed, too ?

Thanks !

<!-- gh-comment-id:1866914416 --> @onlineque commented on GitHub (Dec 21, 2023): Hi @tufanbarisyildirim, thanks a lot for the fix, now the missing eof in block seems to be catched properly. But when I feed the config with some nonsense keyword like "listena" in this example: ``` #user http; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # server { listena 8000; # listen somename:8080; # server_name somename alias another.alias; location / { root html; index index.html index.htm; } } # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } ``` The gonginx says it's valid. But it's not. Testing with nginx -t -c nginx-bad-2.conf gives me expected: ``` 2023/12/21 21:50:36 [emerg] 252611#252611: unknown directive "listena" in /etc/nginx/nginx-bad-2.conf:85 nginx: configuration file /etc/nginx/nginx-bad-2.conf test failed ``` Is it something what can be fixed, too ? Thanks !
Author
Owner

@tufanbarisyildirim commented on GitHub (Dec 21, 2023):

oh hmm, that's an easy one I will add directive validation as well but it actually does not suppose to validate logically because nginx -t can also check the files, permissions etc, but still a good star to add directive validation, will add it.

<!-- gh-comment-id:1867015098 --> @tufanbarisyildirim commented on GitHub (Dec 21, 2023): oh hmm, that's an easy one I will add directive validation as well but it actually does not suppose to validate logically because nginx -t can also check the files, permissions etc, but still a good star to add directive validation, will add it.
Author
Owner

@tufanbarisyildirim commented on GitHub (Dec 21, 2023):

give it a test!

<!-- gh-comment-id:1867025634 --> @tufanbarisyildirim commented on GitHub (Dec 21, 2023): give it a test!
Author
Owner

@onlineque commented on GitHub (Dec 22, 2023):

That's perfect, it works ! May I ask you to release a version for us ? Thanks a lot !

<!-- gh-comment-id:1867825978 --> @onlineque commented on GitHub (Dec 22, 2023): That's perfect, it works ! May I ask you to release a version for us ? Thanks a lot !
Author
Owner

@tufanbarisyildirim commented on GitHub (Dec 22, 2023):

That's perfect, it works ! May I ask you to release a version for us ? Thanks a lot !

sure: https://github.com/tufanbarisyildirim/gonginx/releases/tag/2.0.0

<!-- gh-comment-id:1868048707 --> @tufanbarisyildirim commented on GitHub (Dec 22, 2023): > That's perfect, it works ! May I ask you to release a version for us ? Thanks a lot ! sure: https://github.com/tufanbarisyildirim/gonginx/releases/tag/2.0.0
Author
Owner

@onlineque commented on GitHub (Dec 23, 2023):

I think we can close this issue, it's good enough at least for now ;-) Once again thanks a lot for your help, @tufanbarisyildirim !

<!-- gh-comment-id:1868244686 --> @onlineque commented on GitHub (Dec 23, 2023): I think we can close this issue, it's good enough at least for now ;-) Once again thanks a lot for your help, @tufanbarisyildirim !
Author
Owner

@tufanbarisyildirim commented on GitHub (Dec 23, 2023):

I think we can close this issue, it's good enough at least for now ;-) Once again thanks a lot for your help, @tufanbarisyildirim !

You are welcome, happy to help. will keep this version as base of v2.x.x, thanks for pointing out issues.

<!-- gh-comment-id:1868245494 --> @tufanbarisyildirim commented on GitHub (Dec 23, 2023): > I think we can close this issue, it's good enough at least for now ;-) Once again thanks a lot for your help, @tufanbarisyildirim ! You are welcome, happy to help. will keep this version as base of v2.x.x, thanks for pointing out issues.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/gonginx#14
No description provided.