nginx部署网站服务
安装nginx
安装工具包,依赖包
解压nginx
进入解压目录
编译安装
创建运行nginx服务的用户nginx
进入nginx安装目录
#安装工具包make,gcc;依赖包pcre-devel支持正则 openssl openssl-devel支持加密网站
yum -y install make gcc pcre-devel openssl openssl-devel
tar xf nginx-1.17.6.tar.gz
cd nginx-1.17.6/
#--with-http_ssl_module是安全网站模块
./configure --with-http_ssl_module
make && make install
#创建nginx用户,没有登录服务器的权限
useradd -s /sbin/nologin nginx
cd /usr/local/nginx/
ls
conf html logs sbin
nginx默认网页代码放在html目录
ls html/
50x.html index.html
部署nginx网站
通过nginx默认网页代码html目录部署网站
[root@nginx_web nginx]# echo "zgf-test" > html/index.html
[root@nginx_web nginx]# sbin/nginx
[root@nginx_web nginx]# ss -utnlp | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=4015,fd=6),("nginx",pid=4014,fd=6))
#通过本机localhost访问
[root@nginx_web nginx]# curl http://localhost
zgf-test
#通过ip访问
[root@nginx_web data]# curl 192.168.44.173
zgf-test
匹配访问路径,部署非html目录的网页代码
添加域名(域名为自定义,用域名访问须在本机设置域名解析)
添加匹配访问的路径,访问路径对应的网页代码
#gzip on;
server {
listen 80;
server_name www.test.com; #设置域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#访问路径域名/test1,匹配服务器/data
location /test1 {
alias /data;
#$uri 匹配访问路径本身,本身是html文件,(index.html或以.html结尾的文件)则成功;
#$uri/匹配访问路径是目录,向目录内匹配html文件,目录内有html文件,(index.html或以.html结尾的文件)则成功;不配置$uri/,访问路径为目录时,不向目录内匹配html文件。
#/test1/index3.html 前两个匹配均不成功,最后访问/test1/index3.html,即服务器/data/index.html
try_files $uri $uri/ /test1/index3.html;
}
linux配置域名解析
/etc/hosts文件追加ip和对应的域名
[root@nginx_web nginx]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
#追加ip和对应的域名
192.168.44.173 www.test.com
windows配置域名解析
文本打开C:\Windows\System32\drivers\etc\hosts文件追加ip和对应的域名
192.168.44.173 www.test.com
网页代码/data目录结构,及页面内容
[root@nginx_web data]# echo "/data/index.html" > /data/index.html
[root@nginx_web data]# echo "/data/index1.html" > /data/index1.html
[root@nginx_web data]# echo "/data/index3.html" > /data/index3.html
[root@nginx_web data]# echo "/data/one/index.html" > /data/one/index.html
[root@nginx_web data]# echo "/data/one/index2.html" > /data/one/index2.html
[root@nginx_web data]# tree /data
/data
├── index1.html
├── index3.html
├── index.html
└── one
├── index2.html
└── index.html
1 directory, 5 files
访问域名测试
访问/test1 --> /data/index.html
#访问/test时,nginx内部重定向为/test/,网站访问会看到地址栏补了斜杠/
[root@nginx_web data]# curl http://www.test.com/test1
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.17.6</center>
</body>
</html>
#直接访问/test/
[root@nginx_web data]# curl http://www.test.com/test1/
/data/index.html
访问/test1/index1.html --> /data/index1.html
[root@nginx_web data]# curl http://www.test.com/test1/index1.html
/data/index1.html
访问/test1/one --> /data/one/index.html
#访问/test/one时,nginx内部重定向为/test/one/,网站访问会看到地址栏补了斜杠/
[root@nginx_web data]# curl http://www.test.com/test1/one
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.17.6</center>
</body>
</html>
#直接访问/test/one/
[root@nginx_web data]# curl http://www.test.com/test1/one/
/data/one/index.html
访问/test1/one/index2.html --> /data/one/index2.html
[root@nginx_web data]# curl http://www.test.com/test1/one/index2.html
/data/one/index2.html
访问/test1/abc --> /data/index3.html
#访问路径匹配不到html文件,直接访问配置文件指定的/test1/index3.html
[root@nginx_web data]# curl http://www.test.com/test1/abc
/data/index3.html
[root@nginx_web data]# curl http://www.test.com/test1/abc/
/data/index3.html
访问/test1/abd/index.html --> /data/index3.html
#访问路径匹配不到html文件,直接访问配置文件指定的/test1/index3.html
[root@nginx_web data]# curl http://www.test.com/test1/abd/index.html
/data/index3.html
修改try_files配置,去掉$uri/
[root@nginx_web nginx]# vim conf/nginx.conf
#gzip on;
server {
listen 80;
server_name www.test.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /test1 {
alias /data;
#去掉配置$uri/,去掉后访问路径为目录,nginx不会匹配目录内的html文件,即访问路径不存在html文件,nginx直接访问/test1/index3.html
try_files $uri /test1/index3.html;
}
重新加载配置
[root@nginx_web nginx]# sbin/nginx -s reload
测试访问
[root@nginx_web nginx]# curl http://www.test.com/test1
/data/index3.html
[root@nginx_web nginx]# curl http://www.test.com/test1/
/data/index3.html
[root@nginx_web nginx]# curl http://www.test.com/test1/one
/data/index3.html
[root@nginx_web nginx]# curl http://www.test.com/test1/one/
/data/index3.html
访问直接指向html文件才会成功
[root@nginx_web nginx]# curl http://www.test.com/test1/index.html
/data/index.html
[root@nginx_web nginx]# curl http://www.test.com/test1/one/index.html
/data/one/index.html
[root@nginx_web nginx]# curl http://www.test.com/test1/index1.html
/data/index1.html
[root@nginx_web nginx]# curl http://www.test.com/test1/one/index2.html
/data/one/index2.html