nginx多端口访问及配置访问路径
1.nginx安装
参考 想安装两个nginx的话,在安装的时候指定第二个的安装路径,因为第一个nginx默认安装路径为 /usr/local/nginx 指定路径 ./configure --prefix=/usr/local/nginx1 nginx三大特点:
-
反向代理 负载均衡 动静分离
2.外部访问
nginx.conf文件中的server中listen的端口号需要开放访问
- 查询已经开放的端口 :firewall-cmd --list-port
- 查询某个端口是否开放 :firewall-cmd --query-port=8081/tcp
- 开启端口 :firewall-cmd --zone=public --add-port=8081/tcp --permanent 注:可以是一个端口范围,如1000-2000/tcp
- 重启防火墙 : firewall-cmd --reload
如果使用的是云服务器还需要在云服务器的安全组中放行端口号
3.nginx配置访问路径
server下配置
listen 8081; #配置监听端口号 server_name localhost; #配置访问域名,域名可以有多个,用空格隔开 autoindex on; #打开nginx服务器的目录功能 autoindex_exact_size off; autoindex_localtime on; charset utf-8; #字符集设置
每个location的配置
location / { root html; } location /download { root html; } location /images { expires 7d; #浏览器缓存过期时间为7天 root html; #图片地址,下面的if语句就是判断此目录下是否有响应的文件 proxy_store on; #表示开启缓存 proxy_store_access user:rw group:rw all:rw; #表示用户读写权限 proxy_temp_path html; #此处为文件的缓存路径,这个路径是和url中的文件路径一致的 if ( !-e $request_filename) { proxy_pass http://192.168.0.1; #此处为要被代理的服务器的地址 } }
root表示的是访问资源所在路径,因为没有配置user为root,当前默认处于nginx的安装路径 /user/local/nginx下 root为html,表示访问资源处于绝对路径 /user/local/nginx/html下 当location配置为 location / 时,表示资源就是在 /user/local/nginx/html下 当location配置为 location / download时,资源路径为root配置的路径加上location配置的路径,也即 /user/local/nginx/html/download下 注意:root配置为 html 和 ./html 作用一样,均表示当前目录下开始 root配置为以 / 开头表示从根目录开始 root配置为 . ./ 表示从当前目录的上一级开始 本文中均使用 root html的查找路径
例: 访问http://ip:端口/index.html
访问http://ip:端口/download
访问http://ip:端口/images.scenery.jpg
以下是服务器usr/local/nginx/html文件夹
usr/local/nginx/html/download文件夹
usr/local/nginx/html/images文件夹
4.配置多端口访问
参考 在配置location时候
location /test { root html; proxy_pass http://www.runoob.com/html/html-tutorial.html/; index index.html index.htm; }
加单斜杠会直接根据本机ip+端口号访问到proxy_pass后跟的内容