Setting conf file in nginx from ip to subdomain

March 14, 2018 83 views
Nginx CentOS Ubuntu 16.04


information about my files directory;

/opt/forum/nodebb -----> where i've installed my nodebb

I would like to redirect my droplet ip and port to my subdomain.

I've managed to do sub.mydomain.com:4567 but i don't want to let users see specific port.

i've successfuly installed nginx but i am having a trouble to set things right in conf file.

I did follow this guide to install nodebb on centos.

while i am on putty terminal, when i type *vi /etc/nginx/conf.d/yourdomain.com.conf *command, it gives me a blank page ( sort of ) ( not new window or something ) and i am adding this line as instructed

server {
    listen       80;
    server_name  yourdomain.com; // i did change to sub.mydomain.com;
    location / {
        proxy_pass        http://localhost:4567/; // i didn't change anything here

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_redirect off;
        proxy_buffering   off;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

after i added these lines, i don't know how to save while i am on putty so i am closing putty and it asks to me that " are you sure to close this session" so, i am saying "yes" and reopen putty to making further progress.

when i check the conf file i've created via filezilla, it is compiled so i can't see if it is set as it should be or not.

I mean i can't see this lines on file.

So Am i doing something wrong on putty ? Is there command or something that file i added those lines and save it while i am on putty without closing putty and see how it went ?

If it is not related with putty, how can i fix this ?

nodebb\config.json has set to;

    "url": "http://sub.mydomain.net",
    "secret": "secret thingy",
    "database": "redis",
    "redis": {
        "host": "",
        "port": "6379",
        "password": "password",
        "database": "0"
    "type": "literal"
2 Answers

It's possible that the file isn't being saved when you close PuTTy. By doing so you are terminating the SSH session and forcing vim (the text editor) to quit. Once you run sudo vi /etc/nginx/conf.d/yourdomain.com.conf, press i and then paste in the nginx config. Then, press Escape then :. Type wq and press Enter. This will save the file and exit vim.

You might prefer to use a different editor such as nano which is a bit easier to use. sudo nano /etc/nginx/conf.d/yourdomain.com.conf, paste your config file, and press ctrl-x to save. Follow the prompt the bottom.

Make sure you restart nginx after adding the config file. That should be it.

  • Thank you for your quick reply and sorry about double post! :)

    : and wq works like a charm and i can read the file now however my problem is not solved.

    I can't access to my droplet ip through sub.mydomain.net . Instead i am still accessing through sub.mydomain.net:4567

    In addition, i can't reset, restart nginx by using these commands on terminal;

    quick edit: below this comment can be ignored right now;

    i am getting "500 Internal Server Error" when i typed sub.mydomain.net but i don't get any error from mydomain.net:4567

    * # /etc/init.d/nginx restart
    * # /etc/init.d/nginx reload
    * # service nginx restart
    * # service nginx reload
    * # systemctl restart nginx
    While i used these commands, i am getting errors;
    for service nginx restart command;
    **Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.**
    for service nginx reload
    **Job for nginx.service invalid.**
    then i run this command; nginx -t -c /etc/nginx/nginx.conf
    i got this message; 
    nginx: configuration file /etc/nginx/nginx.conf test failed
    then i run this command; 
    **systemctl status nginx.service**
     nginx.service - The nginx HTTP and reverse proxy server
       Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
       Active: failed (Result: exit-code) since Wed 2018-03-14 18:50:45 UTC; 22s ago
      Process: 1027 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=1/FAILURE)
      Process: 1026 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
     Main PID: 18281 (code=exited, status=0/SUCCESS)
    Mar 14 18:50:45 centos-s-1vcpu-1gb-fra1-01 systemd[1]: Starting The nginx HTT...
    Mar 14 18:50:45 centos-s-1vcpu-1gb-fra1-01 nginx[1027]: nginx: [emerg] unknow...
    Mar 14 18:50:45 centos-s-1vcpu-1gb-fra1-01 nginx[1027]: nginx: configuration ...
    Mar 14 18:50:45 centos-s-1vcpu-1gb-fra1-01 systemd[1]: nginx.service: control...
    Mar 14 18:50:45 centos-s-1vcpu-1gb-fra1-01 systemd[1]: Failed to start The ng...
    Mar 14 18:50:45 centos-s-1vcpu-1gb-fra1-01 systemd[1]: Unit nginx.service ent...
    Mar 14 18:50:45 centos-s-1vcpu-1gb-fra1-01 systemd[1]: nginx.service failed.
    Hint: Some lines were ellipsized, use -l to show in full.

if you just run nginx -t on its own it should give you any errors and what files if the exist

  • when i run, it works okay.

    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful

    i think i might know where i am doing wrong but can't solve it yet.

    It seems i need to set root file directory on nginx conf file but there is no html in the file. Other than that, i have no idea why it doesn't route properly.

    • No in this case you would not need a root directive since you are using NGINX as a proxy server. The 500 error is noting that there is a problem with your backend server, in this case its nodejs for nodebb, you will need to ensure it is up and running.

      • Both are running without any problem.

        I restarted them with commands.


        Works like a charm but not sub.mydomain.net.

        I am going to jump out from windows because this problem :D

Have another answer? Share your knowledge.