{"id":25268258,"url":"https://github.com/ryhkml/exception-filter","last_synced_at":"2025-10-18T00:10:55.979Z","repository":{"id":276987714,"uuid":"930957280","full_name":"ryhkml/exception-filter","owner":"ryhkml","description":"Throwing HTTP exception in Nginx","archived":false,"fork":false,"pushed_at":"2025-04-04T06:54:57.000Z","size":54,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T07:32:46.453Z","etag":null,"topics":["c","errors","http","linux","nginx"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ryhkml.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-11T13:47:11.000Z","updated_at":"2025-04-04T06:55:01.000Z","dependencies_parsed_at":"2025-03-17T10:37:33.277Z","dependency_job_id":null,"html_url":"https://github.com/ryhkml/exception-filter","commit_stats":null,"previous_names":["ryhkml/exception-filter"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryhkml%2Fexception-filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryhkml%2Fexception-filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryhkml%2Fexception-filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryhkml%2Fexception-filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryhkml","download_url":"https://codeload.github.com/ryhkml/exception-filter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247431213,"owners_count":20937913,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","errors","http","linux","nginx"],"created_at":"2025-02-12T10:24:26.580Z","updated_at":"2025-10-18T00:10:55.961Z","avatar_url":"https://github.com/ryhkml.png","language":"C","readme":"# Exception Filter\n\nAn exception-filter is a process that handles specific errors.\nThis concept is based on [NestJS](https://docs.nestjs.com/exception-filters). However, in this implementation, exception-filter simply returns an HTTP response without content.\nBy default, it returns a status code of 204. To return a different status code, use `/:status` as the HTTP status code value.\nFor example: `http://127.0.0.1:10030/500`\n\n## Example in Nginx\n\nInstead of using Nginx's default error page, you can implement exception-filter in Nginx as follows\n\n```\n# /etc/nginx/conf.d/status-code-error.conf\nerror_page 500 501 504 505 506 507 508 510 511 = @err_500;\n```\n\nWhen using directive [error_page](https://nginx.org/en/docs/http/ngx_http_core_module.html#error_page), http status codes 500 through 511 will use the error location named `@err_500`.\nThe next step is to create a proxy pass configuration that points to the exceptipn-filter URL\n\n```\n# /etc/nginx/conf.d/catch-error.conf\nlocation @err_500 {\n    internal;\n    rewrite ^ /500$1 break;\n    proxy_pass http://127.0.0.1:10030;\n    proxy_set_header Host $host;\n\n    include /etc/nginx/conf.d/proxy.conf;\n}\n```\n\nThe location name `@err_500` forwards error processing to `http://127.0.0.1:10030`, which serves as the exception-filter URL.\nThen, simply include it in the domain configuration section below\n\n```conf\n# /etc/nginx/sites-available/domain.com.conf\nserver {\n    listen 443 ssl;\n    listen [::]:443 ssl;\n    http2 on;\n\n    server_name \u003cDOMAIN\u003e;\n\n    include /etc/nginx/conf.d/status-code-error.conf;\n\n    ssl_certificate ...;\n    ssl_certificate_key ...;\n\n    location / {\n        proxy_pass http://\u003cYOUR_APP\u003e;\n        proxy_set_header Host $host;\n        include /etc/nginx/conf.d/proxy.conf;\n    }\n\n    include /etc/nginx/conf.d/catch-error.conf;\n}\n# For 3xx redirects\nserver {\n    listen 80;\n    listen [::]:80;\n\n    server_name \u003cDOMAIN\u003e;\n\n    include /etc/nginx/conf.d/redirect.conf;\n\n    location / {\n        return 301 https://\u003cDOMAIN\u003e$request_uri;\n    }\n\n    include /etc/nginx/conf.d/catch-redirect.conf;\n}\n```\n\n```\n# /etc/nginx/nginx.conf\n...\n...\n...\n\nhttp {\n    charset utf-8;\n    ...\n    ...\n    ...\n\n    # This section is crucial, just create a dummy certificate using OpenSSL.\n    # The server block configuration below is for production.\n    server {\n        listen 80 default_server;\n        listen [::]:80 default_server;\n        listen 443 ssl default_server;\n        listen [::]:443 ssl default_server;\n\n        http2 on;\n\n        server_name _;\n\n        include /etc/nginx/conf.d/status-code-error.conf;\n\n        ssl_certificate /etc/nginx/tls-dummy/fullchain.pem;\n        ssl_certificate_key /etc/nginx/tls-dummy/cert-key.pem;\n\n        include /etc/nginx/conf.d/catch-error.conf;\n\n        return 444;\n    }\n\n    # Comment out the server block configuration above and use the server block configuration below.\n    # The server block configuration below is for the HTTP-01 challenge, a method to verify domain ownership during certificate issuance.\n    #server {\n    #    listen 80 default_server;\n    #    listen [::]:80 default_server;\n\t#\n    #    server_name _;\n\t#\n    #    location / {\n    #        root /usr/share/nginx/html;\n    #        index index.html index.htm;\n    #    }\n    #}\n\n    include /etc/nginx/sites-available/domain.com.conf\n}\n```\n\nIn the above configuration example, if your Nginx returns a 5xx error, it will use the exception-filter to return an error response.\nYou can also add 4xx errors in the same way as the 5xx error example above.\n\n\u003e [!NOTE]\n\u003e\n\u003e Make sure to use the latest version of Nginx.\n\n## Build and Usage\n\n```sh\ngcc -o nob nob.c\n./nob\n```\n\nThere is a list of options available:\n\n| Option         | Default Value | Description                                                       |\n| -------------- | ------------- | ----------------------------------------------------------------- |\n| `-h`, `--help` |               | Display help message                                              |\n| `--max-conn`   | 16            | Specify backlog queue size for the socket                         |\n| `--max-queue`  | 256           | Specify task in queue in thread pool                              |\n| `--max-thread` | CPU(s)        | Specify worker threads that can run simultaneously in thread pool |\n| `--port`       | 10030         | Specify exception-filter port                                     |\n\n```sh\nout/exception-filter --port \u003cPORT\u003e\n```\n\n## Formatter\n\n`.clang-format` is based on [Google](https://google.github.io/styleguide/cppguide.html) style guide\n\n```\nBasedOnStyle: Google\nIndentWidth: 4\nColumnLimit: 120\nAlignArrayOfStructures: Left\nAlignAfterOpenBracket: Align\nBracedInitializerIndentWidth: 4\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryhkml%2Fexception-filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryhkml%2Fexception-filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryhkml%2Fexception-filter/lists"}