Nginx 配置多站点vhost 的方法
假设你想在LinuxNginx中用不同的域名访问不同的目录,这时就要配置多个vhost,具体配置如下,假设网站根目录设定在/var/www/
1、在/var/www/下新建两个目录
/var/www/ushark.net /var/www/ushark.wang
2、编辑/etc/nginx/nginx.conf
http{ include/etc/nginx/mime.types; default_typeapplication/octet-stream; log_formatmain'$remote_addr-$remote_user[$time_local]"$request"' '$status$body_bytes_sent"$http_referer"' '"$http_user_agent""$http_x_forwarded_for"'; access_log/var/log/nginx/access.logmain; sendfileon; #tcp_nopushon; keepalive_timeout65; #gzipon; include/etc/nginx/conf.d/*.conf;#主要是加入此行,如有则忽略 }
3、在/etc/nginx/conf.d下新建两个conf文件,
/etc/nginx/conf.d/ushark.net.conf /etc/nginx/conf.d/ushark.wang.conf
4、复制如下配置信息到两个文件中,只要修改红色部分内容!!!server_name与root保持一致即目录和域名一一对应!!!
server{ listen80; server_namewww.ushark.net; #charsetkoi8-r; #access_log/var/log/nginx/host.access.logmain; root/var/www/ushark.net/; if(!-e$request_filename){#rewrite可根据网站需要增删 rewrite^/(.*)/index.phplast; } location/{ indexindex.phpindex.htmlindex.htm; } #error_page404/404.html; #redirectservererrorpagestothestaticpage/50x.html # error_page500502503504/50x.html; location=/50x.html{ root/var/www/ushark.net/; } #proxythePHPscriptstoApachelisteningon127.0.0.1:80 # #location~\.php${ #proxy_passhttp://127.0.0.1; #} #passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000 # location~*\.php${ fastcgi_indexindex.php; fastcgi_pass127.0.0.1:9000; includefastcgi_params; fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name; fastcgi_paramSCRIPT_NAME$fastcgi_script_name; } #denyaccessto.htaccessfiles,ifApache'sdocumentroot #concurswithnginx'sone # #location~/\.ht{ #denyall; #} }
5、重启Nginx
systemctlrestartnginx
6、编辑/etc/hosts!!!核心步骤!!!
[root@bogon~]#vi127.0.0.1localhost.localdomainlocalhost ::1localhost6.localdomain6localhost6 127.0.0.1www.ushark.net 127.0.0.1www.ushark.wang
总结
以上所述是小编给大家介绍的Nginx配置多站点vhost的方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!