本篇记录了使用Nginx搭建一个简单的静态网站的过程,包括一些简单配置方法,以Ubuntu18.04为例。

以下操作默认在root用户下进行。

安装Nginx

首先用apt下载nginx,默认会安装在/etc/nginx下,

$ sudo apt install nginx

安装完毕后nginx会默认启动,在80端口开放了一个网站,本地访问localhost:80或者在客户端输入ip地址即可看到Welcome to nginx的默认界面。

修改配置文件

接下来我们需要将默认界面修改为自己的网页,假设我们写好的(或者生成的)的网页放在/home/my-website目录下,主页为index.html,我们需要修改nginx的配置文件使其指向该文件夹。

nginx的配置文件主要有三个,一个是nginx根目录下的nginx.conf文件,里面存放nginx的全局配置,一个放在conf.d文件夹下(默认为空),一般在里面创建.conf结尾的配置文件来自定义某个网站的配置,以上两个我们保持不动即可。因为只有一个网站,我们只需要修改sites-available文件夹里的default文件即可。

default文件里有一个默认的server,我们暂时不用动,在下面新加一个server,照着下面的格式填就行,

server {
	listen 80;
	
	#这里填你的域名,无域名的话填ip也可以
	server_name chenshenwei.com www.chenshenwei.com;

	#这里是网站的根目录
	root /home/my-website;
	
	#网站的主页
	index index.html;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}
}

最后检查一下nginx的配置,

$ nginx -t

正确无误后重启nginx服务,

$ nginx -s reload

重新打开网站,就可以发现页面变成自己的网站主页了~

强制跳转域名

有时候我们想要禁止用户通过ip访问,或者为了防止其他人将域名恶意解析到我们的服务器,我们可以设置一个跳转,如果不使用我们的域名访问服务器的话,我们让它强制跳转到我们的域名。

要做的很简单,修改默认服务器的配置即可,

server {
	#有用的就下面四句,其他的注释掉即可
	listen 80 default_server;
	listen [::]:80 default_server;

	#这里_的意思是任意域名或ip
	server_name _;
	
	#强制301跳转至我们的域名
	return 301 http://www.chenshenwei.com;
}

意思就是如果用户输入了除www.chenshenwei.comchenshenwei.com以外的网址访问,就会跳转到http://www.chenshenwei.com

和上面一样重启nginx即可生效。

完整的配置文件如下:

##
# 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.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# 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.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# 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/html;

	# Add index.php to the list if you are using PHP
	#index index.html index.htm index.nginx-debian.html;

	server_name _;
	
	return 301 http://www.chenshenwei.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 PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}

server {
	listen 80;
	
	#这里填你的域名,无域名的话填ip也可以
	server_name chenshenwei.com www.chenshenwei.com;

	#这里是网站的根目录
	root /home/my-website;
	
	#网站的主页
	index index.html;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}
}