'How to access NGINX via mobile device in the local network

I'd like to access my localhost on a mobile device. Currently, I am able to access localhost and it displays my website on the computer, but if I am connected to Wi-Fi I am unable to access localhost on a mobile device, even if I try 127.0.0.1, etc.

worker_processes 1;

events {
    worker_connections 1024;
}

http {
    server {
        listen 80;
        listen 81;
        server_name localhost;
        root /usr/share/nginx/html;
        index index.html index.htm;
        include /etc/nginx/mime.types;
        gzip on;
        gzip_min_length 1000;
        gzip_proxied expired no-cache no-store private auth;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

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



Solution 1:[1]

This is the kind of setup that you do in your router rather than nginx. You can't access your app from another device by localhost:80 because the localhost refers to the own device. I don't know what router you are using, but probably you will find an option to create a virtual server there, so first of all:

  • you must find the ip that was attached to your pc by your router

if you are using linux, open a new terminal and type ifconfig, on windows open the cmd and type ipconfig, your ip will looks like this:

ip

  • So after that, create the virtual server

go to your router, find the option to create the virtual server and do so with the ip that you got earlier, probably it will ask you to provide a port, use the same of your nginx, in your case 80.

Now whenever some device connected in your router tries to connect to the ip of your pc at port 80 nginx should serve your app.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1