Setting conf file in nginx from ip to subdomain

March 14, 2018 479 views
Nginx CentOS Ubuntu 16.04
Kayrah
By:
Kayrah

Hello,

information about my files directory;

/etc/nginx
/etc/nginx/conf.d
/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.
https://www.rosehosting.com/blog/how-to-install-nodebb-on-a-centos-7-vps/

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": "127.0.0.1",
        "port": "6379",
        "password": "password",
        "database": "0"
    },
    "type": "literal"
}
3 Answers
Kayrah March 19, 2018
Accepted Answer

@kamaln7

Thanks for your reply and your time.!

I did run sudo tail -n 50 /var/log/nginx/error.log this command and i got this;

2018/03/19 18:05:49 [crit] 7468#0: *1 connect() to 127.0.0.1:4567 failed (13: Permission denied) while connecting to upstream, 
client: XX.190.XXX.115, server: forum.mydomain.net, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:4567/", 
host: "forum.mydomain.net"
2018/03/19 18:05:49 [crit] 7468#0: *1 connect() to [::1]:4567 failed (13: Permission denied) while connecting to upstream, 
client: XX.190.XXX.115, server: forum.mydomain.net, request: "GET / HTTP/1.1", upstream: "http://[::1]:4567/", 
host: "forum.mydomain.net"
2018/03/19 18:05:49 [error] 7468#0: *1 no live upstreams while connecting to upstream, 
client: XX.190.XXX.115, server: forum.mydomain.net, request: "GET /favicon.ico HTTP/1.1", 
upstream: "http://localhost/favicon.ico", host: "forum.mydomain.net", referrer: "http://forum.mydomain.net/"

I have no error.log file on /opt/nodebb ( where i've installed )

I did some search on google and it seems nginx related but im not sure how to solve to this and which file edit to solve.

  • Update: I looked around and found some posts on this. CentOS's SELinux might be blocking nginx from connecting to NodeBB. Does this command fix it?

    sudo setsebool -P httpd_can_network_connect 1
    

    ——————————

    I'm not sure why nginx's is getting a Permission denied error while trying to connect to NodeBB. That's strange. What happens if you replace

            proxy_pass        http://localhost:4567/;
    

    with

            proxy_pass        http://127.0.0.1:4567/;
    

    and add the following line (if it does not exist, otherwise update the value) to NodeBB's config.json:

    "bind_address": "127.0.0.1",
    

    and restart NodeBB and Nginx? Setting bind_address to 127.0.0.1 will make NodeBB accessible only from within the Droplet (through nginx) so expect to not be able to browse to http://ip:4567.

    • I can't thank you enough!

      This command works like a charm!

      sudo setsebool -P httpd_can_network_connect 1
      

      I have no route problem now. Thank you so much for your assistance!

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.

        http://sub.mydomain.net:4567
        dropletip:4567

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

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

        • Is nginx running? Do you get an error when you browse to sub.domain.net or nginx's default page? What happens when you browse to http://ip without port 4567?

          • Nginx is running without any problem if i dont add this file and commands inside on it.

            sudo nano /etc/nginx/conf.d/beta1.dizitavsiye.net.conf

            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";
                }
            }
            

            If i don't add this file and these commands, when i browse http://forum.mydomain.net

            i got this screen on my browser;

            ---> https://s13.postimg.org/6h4gxvxwn/fedora_nginx.png

            if i do create sudo nano /etc/nginx/conf.d/beta1.dizitavsiye.net.conf this file,

            I am getting '502 Bad Gateway' error while i am browsing my subdomain.

          • @Kayrah that error means that nginx did not receive a proper response from http://localhost:4567. Either NodeBB isn't running or is returning an error. Check nginx's error logs as well as NodeBB's. See if there's anything in there that might help.

            sudo tail -n 50 /var/log/nginx/error.log
            

            This command will print the last 50 lines of nginx's error log. Run the same one on NodeBB's error log, It should be somewhere in /opt/forum/nodebb.

Have another answer? Share your knowledge.