nginx部署网站服务
安装nginx
安装工具包,依赖包
解压nginx
进入解压目录
编译安装
创建运行nginx服务的用户nginx
进入nginx安装目录
yum -y install make gcc pcre-devel openssl openssl-devel
tar xf nginx-1.17.6.tar.gz
cd nginx-1.17.6/
./configure --with-http_ssl_module
make && make install
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]
[root@nginx_web nginx]
[root@nginx_web nginx]
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=4015,fd=6),("nginx",pid=4014,fd=6))
[root@nginx_web nginx]
zgf-test
[root@nginx_web data]
zgf-test
匹配访问路径,部署非html目录的网页代码
添加域名(域名为自定义,用域名访问须在本机设置域名解析) 添加匹配访问的路径,访问路径对应的网页代码
server {
listen 80;
server_name www.test.com;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /test1 {
alias /data;
try_files $uri $uri/ /test1/index3.html;
}
linux配置域名解析
/etc/hosts文件追加ip和对应的域名
[root@nginx_web nginx]
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
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]
[root@nginx_web data]
[root@nginx_web data]
[root@nginx_web data]
[root@nginx_web data]
[root@nginx_web data]
/data
├── index1.html
├── index3.html
├── index.html
└── one
├── index2.html
└── index.html
1 directory, 5 files
访问域名测试
访问/test1 --> /data/index.html
[root@nginx_web data]
<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>
[root@nginx_web data]
/data/index.html

访问/test1/index1.html --> /data/index1.html
[root@nginx_web data]
/data/index1.html

访问/test1/one --> /data/one/index.html
[root@nginx_web data]
<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>
[root@nginx_web data]
/data/one/index.html

访问/test1/one/index2.html --> /data/one/index2.html
[root@nginx_web data]
/data/one/index2.html

访问/test1/abc --> /data/index3.html
[root@nginx_web data]
/data/index3.html
[root@nginx_web data]
/data/index3.html

访问/test1/abd/index.html --> /data/index3.html
[root@nginx_web data]
/data/index3.html
 修改try_files配置,去掉$uri/
[root@nginx_web nginx]
server {
listen 80;
server_name www.test.com;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /test1 {
alias /data;
try_files $uri /test1/index3.html;
}
重新加载配置
[root@nginx_web nginx]
测试访问
[root@nginx_web nginx]
/data/index3.html
[root@nginx_web nginx]
/data/index3.html
[root@nginx_web nginx]
/data/index3.html
[root@nginx_web nginx]
/data/index3.html
    访问直接指向html文件才会成功
[root@nginx_web nginx]
/data/index.html
[root@nginx_web nginx]
/data/one/index.html
[root@nginx_web nginx]
/data/index1.html
[root@nginx_web nginx]
/data/one/index2.html

  
|