nginx configuration uses ip address to access the website

 

I have been writing a website for a friend these days, but his domain name has not been successfully registered. I want to access it using the server IP first. The configuration was successful before, but when I tried to configure it again this time, the result was... It was a bit difficult. I feel like the high school teacher said it well. ——"A good memory is worse than a bad writing", so I have to record everything I say this time.

The web server chosen is nginx.

server
    {
        listen 80;  #监听端口号
        #server_name xxx.com;  #你的域名
        location / {
          root  /myweb; #你的静态页面所在目录
          index index.html;  #你的静态页面名字
        }
        include enable-php.conf;  #默认的不用管
    }

After the above configuration, you can access the html page you deployed on the server through ip/, such as xxx.xxx.xxx.xxx/.

Note that there is a big pit here. The root under location/ above should not be configured in the form of /root/xxx, because nginx seems to protect the files under /root of the server by default and does not allow access. Otherwise, error 403 will always be reported when accessing xxx.xxx.xxx.xxx/!

If your static page's js uses get/post requests to access the backend interface, it doesn't matter. You can complete it through the following configuration!

server
    {
        listen 80;
        #server_name xxx.com;
        location / {
          root  /myweb;
          index index.html;
        }
         location /my {
            #反向代理访问后端接口
            proxy_pass http://localhost:8081/upload;
        }
        include enable-php.conf;
    }

If the backend interface you want to access in front-end js is http://123.xx.xx.xx:8081/upload, then you only need to implement the above configuration in nginx, and then change the request in js to http://123.xx.xx.xx/my That’s it!

nginx does not allow direct requests to http://123.xx.xx.xx:8081/upload, so you need to use the upload interface under the /my reverse proxy 8081 port.

Perfectly!!!

 

 

Guess you like

Origin blog.csdn.net/m0_63080216/article/details/131643419