ginx配置默认首页(index.htnl index.htm)全流程(包含遇到问题的解决) ...

本文详细介绍了如何配置Nginx以指向特定目录下的index.html作为默认首页,包括配置流程、常见问题解决及权限调整。同时,分享了环境变量配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ginx配置默认首页(index.htnl index.htm)全流程(包含遇到问题的解决)
需求:

自己有个域名,原来直接扔在了服务器的文件夹里(根据客服人员指导),自己玩了一遍nginx的安装部署等操作之后,域名的指向发生了改变,到了nginx成功的界面。

自己抱着极大的好奇心来配置nginx,已达到我能访问到我的主页的样子,当然啦。做个域名主页对我来说最主要的作用就是学(装)习(逼)。

解决方案:

google找到了其中的方法,大概就是修改nginx的配置文件了,让其index指向特定目录下的index.html等主页文件。

先开始一顿配置nginx的操作:

1,找到nginx.conf文件的位置,并将其用vi命令打开。

2,在其中设置自己主页的路径,和主页名称。

复制代码

打开配置文件

cd /usr/local/nginx/conf
vi nginx.conf

配置nginx

listen 80;
location / {
index login.html;
root root/home;
}
复制代码
我们仅仅配置是不行的,还需要重新启动一下,这样子我们的配置才可以生效。

复制代码

启动nginx

cd usr/local/nginx/sbin
./nginx

重启nginx

cd /usr/local/nginx/sbin
./nginx -s reload
复制代码
我们重启了之后,再次通过域名指向我们ip地址的时候,nginx欢迎的界面没有了,取而代之的是403 is forbidden了。

google一下发现导致403的原因很多:

1,指定的文件夹没有这个首页的文件。(index.html)

2,权限问题,如果nginx没有web目录的操作权限,也会出现403错误。

我的是因为权限问题导致的,知道了原因就很好解决啦。

解决权限问题

chmod -R 755 /root/home
之后我们需要,把nginx的启动用户改成目录的所属用户方法:

在打开配置文件之后配置,我的简单粗暴

user root;
最后:

表面上成功的实现了功能。其实还有很多很多东西自己不懂。

最后的最后还涉及到了环境变量的配置。

配置nginx环境变量:

复制代码

打开配置文件

vi /etc/profile

增加配置

export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin

保存 - 》 执行

source /etc/profile
复制代码
原文地址https://www.cnblogs.com/tujietg/p/10753041.html

user www www; worker_processes auto; error_log /www/wwwlogs/nginx_error.log crit; pid /www/server/nginx/logs/nginx.pid; worker_rlimit_nofile 100000; stream { log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time'; access_log /www/wwwlogs/tcp-access.log tcp_format; error_log /www/wwwlogs/tcp-error.log; include /www/server/panel/vhost/nginx/tcp/*.conf; } events { use epoll; worker_connections 51200; multi_accept on; } http { include mime.types; # 修正 MIME 类型定义(移除重复项) types { font/truetype ttf; font/opentype otf; font/woff woff; font/woff2 woff2; } # 移除重复的 default_type # default_type application/font-sfnt; // 注释掉这行 default_type application/octet-stream; #include luawaf.conf; include proxy.conf; default_type application/octet-stream; server_names_hash_bucket_size 512; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 300m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 3000; fastcgi_send_timeout 3000; fastcgi_read_timeout 3000; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\."; limit_conn_zone $binary_remote_addr zone=perip:10m; limit_conn_zone $server_name zone=perserver:10m; server_tokens off; access_log off; server { listen 888; server_name phpmyadmin; index index.html index.htm index.php; root /www/server/phpmyadmin; location ~ /tmp/ { return 403; } #error_page 404 /404.html; include enable-php.conf; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } # 修正后的字体配置块 location ~* \.(ttf|otf|woff2?)$ { access_log /www/wwwlogs/font_access.log; error_log /www/wwwlogs/font_error.log; root /www/wwwroot/yinzhang.jinshelby.com; rewrite ^/xcx/font/(.*)$ /public/xcx/font/$ 1 break; # 修正:$ 1 # CORS 头 add_header Access-Control-Allow-Origin * always; add_header Access-Control-Allow-Methods 'GET, OPTIONS' always; add_header Access-Control-Allow-Headers 'Range' always; add_header Access-Control-Expose-Headers 'Content-Length,Content-Range' always; # 缓存 expires 1y; add_header Cache-Control "public, max-age=31536000, immutable"; # 字节范围支持(安卓必需) add_header Accept-Ranges bytes; if ($ request_method = 'OPTIONS') { # 修正:$ request_method return 204; } } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /\. { deny all; } access_log /www/wwwlogs/access.log; } include /www/server/panel/vhost/nginx/*.conf; } 还是报错 : ginx version: nginx/1.20.2 nginx: [warn] duplicate extension "woff", content type: "font/woff", previous content type: "font/woff" in /www/server/nginx/conf/nginx.conf:29 nginx: [warn] duplicate extension "woff2", content type: "font/woff2", previous content type: "font/woff2" in /www/server/nginx/conf/nginx.conf:30 nginx: [emerg] "default_type" directive is duplicate in /www/server/nginx/conf/nginx.conf:41 nginx: configuration file /www/server/nginx/conf/nginx.conf test failed
最新发布
07-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值