CentOS6使用nginx搭建web网站服务的方法
利用CentOS6搭建简易的web服务
提示:其中没有涉及到MySQl、MongoDB的安装和使用,包括docker容器等,使用nginx反向代理静态服务
centOS服务器
可以选用国外或者国内的服务器,这里只展示centOS系统配置,本人实在window系统下完成下列操作
选购一台合适的云服务器,系统为centOS
在window系统下安装xshell和xftp,保证可以系统访问centOS系统
使用xshell连接上云服务器,使用超级管理员root登录
登录完成之后安装nginx
下面是需要下载的文件:
- 首先安装wget#yuminstallwget
- nginx以来与gcc的编译环境#yuminstallgcc-c++
- nginx的http模块需要使用pcre来解析正则表达式#yum-yinstallpcrepcre-devel
- 依赖的解压包#yum-yinstallzlibzlib-devel
- 下载nginx压缩包#wget-chttps://nginx.org/download/nginx-1.10.3.tar.gz
解压与安装:
- 解压nginx#tar-zxvfnginx-1.10.3.tar.gz
- 进入nginx目录#cdnginx-1.10.3
- 对nginx的源码进行编译#./configure
- 开始编译#make
- 继续编译#makeinstall
- 查看nginx安装的目录#whereisnginx它会告诉你nginx在哪,nginx的命令在/usr/local/nginx/sbin目录下
nginx命令:
nginx命令开启#./nginx
停止#./nginx-sstop
#./nginx-squit
重启#./nginx-sreload
开启nginx#./nginx必须在nginx的安装目录下的sbi文件开启,当然也可全局配置
查看是否开启nginx#psaux|grepnginx
nginx配置
以下是nginx配置的部分参考
#httpserver
server{
listen80;
server_namewww.didiheng.com;
#charsetkoi8-r;
#access_loglogs/host.access.logmain;
access_logoff;#缓存日志关闭
server_tokensoff;
tcp_nopushon;
tcp_nodelayon;
gzipon;#gzip开启
gzip_comp_level6;#gzip比率
gzip_typestext/plainapplication/x-javascripttext/cssapplication/xmltext/javascriptapplication/x-httpd-phpimage/jpegimage/gifimage/png;
proxy_connect_timeout5;#缓存链接
proxy_read_timeout60;
proxy_send_timeout5;
proxy_buffer_size16k;
proxy_buffers464k;
proxy_busy_buffers_size128k;
proxy_temp_file_write_size128k;
location/{
root/www;#此处绝对地址
indexindex.htmlindex.htm;
try_files$uri$uri//index.html;//使用客户端路由需配置
rewrite^(.*)$https://$host$1permanent;/强制定向https
}
#error_page404/404.html;
#redirectservererrorpagestothestaticpage/50x.html
#
error_page500502503504/50x.html;
location=/50x.html{
roothtml;
}
#httpsserver
server{
listen443ssl;
server_namewww.didiheng.com;
ssl_certificateserver.crt;
ssl_certificate_keyserver.key;
ssl_session_cacheshared:SSL:1m;
ssl_session_timeout5m;
ssl_ciphersHIGH:!aNULL:!MD5;
ssl_prefer_server_cipherson;
access_logoff;
server_tokensoff;
tcp_nopushon;
tcp_nodelayon;
expiresepoch;
gzipon;
gzip_comp_level6;
gzip_typestext/plainapplication/x-javascripttext/cssapplication/xmltext/javascriptapplication/x-httpd-phpimage/jpegimage/gifimage/png;
proxy_connect_timeout5;
proxy_read_timeout60;
proxy_send_timeout5;
proxy_buffer_size16k;
proxy_buffers464k;
proxy_busy_buffers_size128k;
proxy_temp_file_write_size128k;
location/{
root/www;
indexindex.htmlindex.htm;
try_files$uri$uri//index.html;
}
}
}
在以上配置中我直接将https开启了,若没有相关的ssl配置,请将https服务注释使用#即可
修改之后重启nginx
#./nginx-sreload
使用serverIP或域名访问访问
github原址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。