Question

How to configure my domain in nginx

Hi, I am trying to upload my web application then I’ve used your tutorial “https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-8” in the Step - 6 as example I created my domain.conf but after to saving and closing the file I tested the nginx to see syntax errors and I getting this error:

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/conf.d/itcmedbr.com.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed

Below my conf file:

vi /etc/nginx/conf.d/itcmedbr.com.conf
1 server {
      2         listen 80;
      3         listen [::]:80;
      4
      5         root /var/www/itcmedbr.com/html;
      6         index index.jsp;
      7
      8         server_name itcmedbr.com www.itcmedbr.com;
      9
     10         location / {
     11                 try_files $uri $uri/ =404;
     12         }
     13 }
~

This is my nginx.conf:

      1 # For more information on configuration, see:
      2 #   * Official English Documentation: http://nginx.org/en/docs/
      3 #   * Official Russian Documentation: http://nginx.org/ru/docs/
      4
      5 user nginx;
      6 worker_processes auto;
      7 error_log /var/log/nginx/error.log;
      8 pid /run/nginx.pid;
      9
     10 ##
     11 # Virtual hosts configs
     12 ##
     13 include /etc/nginx/conf.d/*.conf;
     14 include /etc/nginx/sites-enabled/*;
     15
     16 # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
     17 include /usr/share/nginx/modules/*.conf;
     18
     19 events {
     20     worker_connections 1024;
     21 }
     22
     23 http {
     24     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
     25                       '$status $body_bytes_sent "$http_referer" '
     26                       '"$http_user_agent" "$http_x_forwarded_for"';
     27
     28     access_log  /var/log/nginx/access.log  main;
     29
     30     sendfile            on;
     31     tcp_nopush          on;
     32     tcp_nodelay         on;
     33     keepalive_timeout   65;
     34     types_hash_max_size 2048;
     35
     36     include             /etc/nginx/mime.types;
     37     default_type        application/octet-stream;
     38     include /etc/nginx/sites-enabled/*.*;
     39
     40     # Load modular configuration files from the /etc/nginx/conf.d directory.
     41     # See http://nginx.org/en/docs/ngx_core_module.html#include
     42     # for more information.
     43     include /etc/nginx/conf.d/*.conf;
     44
     45     server {
     46         listen       80 default_server;
     47         listen       [::]:80 default_server;
     48
     49         server_name  _;
     50         root         /usr/share/nginx/html;
     51     }
     52
     53     server {
     54         server_name  itcmedbr.com www.itcmedbr.com;
     55         root         /var/www/;
     56
     57         # Load configuration files for the default server block.
     58         include /etc/nginx/default.d/*.conf;
     59
     60         location / {
     61             proxy_pass http://myipaddress:8080;
     62
     63         }
     64
     65         error_page 404 /404.html;
     66             location = /40x.html {
     67         }
     68
     69         error_page 500 502 503 504 /50x.html;
     70             location = /50x.html {
     71         }
     72     }
     73
     74 # Settings for a TLS enabled server.
     75 #
     76 #    server {
     77 #        listen       443 ssl http2 default_server;
     78 #        listen       [::]:443 ssl http2 default_server;
     79 #        server_name  _;
     80 #        root         /usr/share/nginx/html;
     81 #
     82 #        ssl_certificate "/etc/pki/nginx/server.crt";
     83 #        ssl_certificate_key "/etc/pki/nginx/private/server.key";
     84 #        ssl_session_cache shared:SSL:1m;
     85 #        ssl_session_timeout  10m;
     86 #        ssl_ciphers PROFILE=SYSTEM;
     87 #        ssl_prefer_server_ciphers on;
     88 #
     89 #        # Load configuration files for the default server block.
     90 #        include /etc/nginx/default.d/*.conf;
     91 #
     92 #        location / {
     93 #        }
     94 #
     95 #        error_page 404 /404.html;
     96 #            location = /40x.html {
     97 #        }
     98 #
     99 #        error_page 500 502 503 504 /50x.html;
    100 #            location = /50x.html {
    101 #        }
    102 #    }
    103
    104 }

I realy need help. Thanks and best regards.


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.

KFSys
Site Moderator
Site Moderator badge
February 21, 2024

Heya @cezarapulchro,

Try the following Nginx configuration file

server {
    listen 80;
    listen [::]:80;

    root /var/www/itcmedbr.com/html;
    index index.jsp;

    server_name itcmedbr.com www.itcmedbr.com;

    location / {
        try_files $uri $uri/ /index.html;
    }

    # Additional configuration for handling other static files, if any

    # Error pages configuration (optional)
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    # Logging (optional but recommended)
    access_log /var/log/nginx/itcmedbr.com.access.log;
    error_log /var/log/nginx/itcmedbr.com.error.log;
}

That should do the trick.

I noticed the numbers on the left(line numbers). Make sure those do not actually exist in the file as well.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel