访问
在安装并启动Nginx
之后,便可以通过浏览器来访问Nginx
了。Nginx
的配置文件中默认设置的监听端口是80。
在http
协议中,80是默认端口。例如访问百度网站时,在浏览器地址栏中输入了https//baidu.com
,实际上访问的是https//baidu.com:80
。
默认端口(80)
默认情况下,云服务器会自动开放80端口。
在浏览器地址栏中输入ip访问,如果访问成功,则会在页面上展示Welcome to nginx!
的字样。
指定端口(8080)
如果需要让nginx
监听指定端口,例如8080,就需要修改nginx.conf
文件。
同时还需要确保云服务器本身已经对外开放了8080端口。如果没有开放,就需要手动设置开放。
# 进入到nginx的配置目录
$ cd /usr/local/nginx/conf
# 编辑nginx.conf配置文件
$ vim nginx.conf
nginx.conf
将server
指令块中的80修改为8080,保存并退出。
...
http {
...
server {
location 8080;
...
}
}
...
# 切换到sbin目录
$ cd /usr/local/nginx/sbin
# 测试nginx配置文件语法是否正确,出现以下内容,表示正确
$ ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# 重新加载配置文件,使得修改起效
$ ./nginx -s reload
# 如果不是Centos7,那就没有firewall-cmd命令,应该去使用对应版本的防火墙命令
# 查看服务器是否已经开放8080端口,no表示未开放,yes表示已开放
$ firewall-cmd --query-port=8080/tcp
no
# 永久开放8080端口,success表示已经添加成功
$ firewall-cmd --add-port=8080/tcp --permanent
success
# 重新加载,使得修改起效
$ firewall-cmd --reload
success
# 再次查看服务器是否已经开放8080端口,no表示未开放,yes表示已开放
$ firewall-cmd --query-port=8080/tcp
yes
如果购买的是阿里云/腾讯云等云服务器厂商的服务器,那么还需要在安全组中配置8080端口入站规则,否则请求会被拦截。
如果已经完成上述步骤,并且配置了对应的安全组。此时在浏览器地址栏中输入ip:8080
访问,如果配置无误,则会在页面上展示Welcome to nginx!
的字样。