Ngxin禁止IP+端口号访问系统,设置只能通过域名访问

  1. 下载nginx

    yum install nginx
    
  2. 配置nginx

    查看配置文件的位置:

    [root@VM-4-8-centos ~]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
    vim /etc/nginx/nginx.conf
    

    配置文件按照以下修改:

    server{
    	listen 80 default_server;
    	server_name _;
    	return 403;
    }
    server{
    	listen 80;
    	server_name nanaco.cn; # 你的域名
    	location / {
    		proxy_pass http://127.0.0.8090;
    	}
    }
    

    注意此处上下两个server的顺序,不能交换。项目端口号为8090,因此映射到8090;

  3. 对80端口进行防火墙配置

    firewall-cmd --zone=public --add-port=80/tcp --permanent
    systemctl restart firewalld.service
    
  4. 若提示未安装firewalld,则先安装:

    yum install firewalld
    systemctl unmask firewalld
    systemctl enable firewalld
    systemctl start firewalld
    
  5. 启动nginx,启动nginx之前需要先将8090端口的项目启动:

    nohup java -jar halo.jar &
    systemctl start nginx
    

    若启动失败,查看是否80端口已被占用,把占用的程序关掉再重新启动。

  6. 访问页面,此时ip+端口号已经不能访问,只能域名访问。

  7. Nginx常用指令

    查看nginx状态:systemctl status nginx.service
    
    启动nginx:systemctl start nginx
    
    重启nginx:systemctl restart nginx
    
    查看配置:nginx -t
    
    开机自启动:systemctl enable nginx
    

版权声明:本文为m0_46144891原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。