二级域名配置以及nginx解析二级域名到html页面
创始人
2024-02-20 04:08:26
0

此文章适合发布前端项目使用,如果想要配置二级域名到后端服务,可以查看这篇文章:nginx配置二级域名 - 简书

在阿里云上配置二级域名,就是添加一条记录就可以了,超级简单,不懂的可以看后面的解释说明,比如我这里添加了一个second.1024shen.com为二级域名,主域名是1024shen.com

我们的主域名页面是:

我的nginx配置目录内容是:

 

我们的nginx.conf 默认配置是:

user www-data;
worker_processes auto;
pid /run/nginx.pid;events {worker_connections 768;# multi_accept on;
}http {### Basic Settings##sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;# server_tokens off;# server_names_hash_bucket_size 64;# server_name_in_redirect off;include /etc/nginx/mime.types;default_type application/octet-stream;### SSL Settings##ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLEssl_prefer_server_ciphers on;### Logging Settings##access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log;### Gzip Settings##gzip on;gzip_disable "msie6";# gzip_vary on;# gzip_proxied any;# gzip_comp_level 6;# gzip_buffers 16 8k;# gzip_http_version 1.1;# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;### Virtual Host Configs##include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;
}#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
# 
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

 可以看到里面引入了:

nginx 会从 /etc/nginx/conf.d 中加载以 .conf 结尾的配置文件

nginx 会从 /etc/nginx/sites-enabled 中加载任何名称的配置文件

 sites-available 中拥有名为 default 的配置文件,打开即可在该文件开头看到 nginx packaging team 的说明:

 In most cases, administrators will remove this file from sites-enabled/ and leave it as reference inside of sites-available where it will continue to be updated by the nginx packaging team.

通常情况下,网站管理员会将此文件的链接从 sites-enabled 中删除,并将其作为 sites-available 中其他文件的参考,nginx packaging team 将持续对此文件进行更新。

也就是说,文件夹下的 default 为网站配置文件的参考,由于在 nginx 更新时,default 会一同被更新以展示配置文件的变化,所以在配置网站时,不应该直接修改此文件,需要复制为新文件,再进行修改。

sites-available 则是用于存放网站的配置文件,意为可用的网站列表,用于在需要时链接到 sites-enabled 中作为需要启用的网站。

sites-enabled 中则只拥有 sites-available 文件夹下 default 的软链接,结合前面得出:

sites-enabled 下的文件,会作为 nginx.conf 的一部分加载
sites-enabled 下的用于存放 sites-available 中文件的软连接
sites-enabled 意为已开启的网站,将 sites-available 中的配置文件链接到此处,以使配置文件被 nginx 加载。

sites-available 与 sites-enabled 使我们能够进行模块化配置,当我们希望增加新网站时,我们可以在 sites-available 中创建新配置文件;当我们需要关闭某个站点时,我们可以在 sites-enabled 中将链接移除,这在某种程度是提高了 nginx 的管理效率。

所以我们只需要在sites-available创建一个新的配置文件,直接复制default,然后修改里面的内容:

 

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
### Default server configuration
#
server {listen 80;listen [::]:80;# SSL configuration## listen 443 ssl default_server;# listen [::]:443 ssl default_server;## Note: You should disable gzip for SSL traffic.# See: https://bugs.debian.org/773332## Read up on ssl_ciphers to ensure a secure configuration.# See: https://bugs.debian.org/765782## Self signed certs generated by the ssl-cert package# Don't use them in a production server!## include snippets/snakeoil.conf;root /var/www/second;# Add index.php to the list if you are using PHPindex index.html index.htm index.nginx-debian.html;server_name second.1024shen.com;location / {# First attempt to serve request as file, then# as directory, then fall back to displaying a 404.try_files $uri $uri/ =404;}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#	include snippets/fastcgi-php.conf;##	# With php7.0-cgi alone:#	fastcgi_pass 127.0.0.1:9000;#	# With php7.0-fpm:#	fastcgi_pass unix:/run/php/php7.0-fpm.sock;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#	deny all;#}
}# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

注意修改这两个地方:root后面跟上静态文件目录,server_name 后面跟上二级域名

然后在/var/www路径下创建一个second文件夹,并创建一个index.html页面:


Document

这是second.1024shen.com页面!!!!!

然后重新加载nginx配置:

 service nginx reload 

然后访问子域名:http://second.1024shen.com/

 

相关内容

热门资讯

游客身边的法律“向导”|松北法... “请大家有序排队,注意冰面防滑……”12月17日,备受期待的哈尔滨冰雪大世界盛装启幕,松北区法院旅游...
法院不判离婚,发“感情修复通知... 近日,一份由“武汉某区人民法院”出具的“夫妻感情修复通知书”在网上流传,引发热议。 对此,不少网...
立法新动向:全国人大常委会会议... 十四届全国人大常委会第十九次会议12月22日至27日在北京举行。此次会议将对国有资产法草案和商标法修...
为与前男友打官司,女子网寻“律... 因与前男友发生纠纷,女子在网络上寻找律师,却不曾想落入骗子圈套。假律师多次以“案件办理费”“疏通打点...
自贡沿滩镇:亲情裂痕巧弥合,网... 近日,自贡市沿滩区沿滩镇飞跃村一名网格员凭借耐心细致的工作,成功调解了一起因耕地边界争议引发的亲属纠...
香港一地天降多张面额500元钞... 12月17日上午,香港警方接获报案称,观塘鲤鱼门邨鲤生楼有钞票从天而降,散落在大楼周围的地面,部分有...
国有资产法草案拟初审:规定国有... 国有资产法草案拟明确国有资产管理制度,并规定国有资产报告、监督制度。 12月19日,全国人大常委会法...
全球报道仅2例!广西发现稀有血... 12月17日,自治区妇幼保健院(广西儿童医疗中心)医院检验科在临床工作中,发现并成功鉴定出一例稀有血...