使用Nginx在个人服务器上搭载静态网站
Nginx的作用
Nginx是一款高性能的Web服务器和反向代理服务器,其主要作用如下:
-
提供Web服务器功能:Nginx可以作为静态或动态内容的Web服务器,为Web应用程序提供服务。它可以处理静态文件和动态请求,支持FastCGI、uWSGI、SCGI和WSGI等协议,并提供缓存和压缩等功能。
-
反向代理服务器:Nginx还可以作为反向代理服务器,用于将请求转发到后端服务器(如应用服务器、数据库服务器等),并将响应返回给客户端。反向代理可以提高Web应用程序的可靠性、安全性和可扩展性。
-
负载均衡器:Nginx可以作为负载均衡器,将请求分发到多个后端服务器,以提高Web应用程序的性能和可扩展性。
-
HTTPS代理服务器:Nginx还可以作为HTTPS代理服务器,用于为后端服务器提供SSL/TLS加密功能,保护Web应用程序的安全性。
总之,Nginx是一个灵活、高效、可靠的Web服务器和反向代理服务器,具有处理大量并发连接和高负载的能力,被广泛用于Web应用程序的部署和扩展。
nginx配置文件简介
Nginx配置文件通常在 `/etc/nginx/`下, 常见文件组织结构如下:
/etc/nginx/ ├── conf.d ├── fastcgi.conf ├── fastcgi_params ├── koi-utf ├── koi-win ├── mime.types ├── modules-available ├── modules-enabled ├── nginx.conf // 顶层配置文件,包括worker,log位置等 ├── proxy_params ├── scgi_params ├── sites-available │ ├── default │ └── starriest.top ├── sites-enabled // 启用的sites │ └── starriest.top ├── snippets │ ├── fastcgi-php.conf │ └── snakeoil.conf ├── uwsgi_params └── win-utf
可以查看/etc/nginx/sites-enabled/default文件,进行配置
ngxin log一般在/var/log/nginx/access.log和/var/log/nginx/error.log中
默认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 _;
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.4-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;
#}
}
# 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;
# }
#}
-
server定义了一个server block, 可以处理对应端口接受到的请求, 并进行相应返回。
-
nginx会根据server_name和请求Header中的host选择匹配到哪个具体的server block, 如果server_name是_的话,那么没有被匹配到请求就会被转发到此server.
-
listen定义了server对应的端口,如果是default_server的话,那么该端口下没有被server_name匹配的请求会被转发到该defult server, 无论default server的server_name是否是_,一个端口只能有一个default server, 如果没有指定default server, 那么按照顺序,第一个server会被作为defualt server.
-
index 默认情况下,nginx的index指令的值为`index.html index.htm`。这意味着当客户端请求一个目录时,Nginx将首先检查该目录下是否存在index.html文件,如果不存在,则继续检查是否存在index.htm文件。如果两个文件都不存在,则Nginx将返回目录列表或403 Forbidden错误。
-
location
Nginx配置TLS协议(启用HTTPS)
要在Nginx中启用SSL加密,需要执行以下步骤:
-
为服务器获取SSL证书:您需要从信任的证书颁发机构(CA, 例如ZeroSSL)购买SSL证书,或使用自签名证书来启用SSL。这通常需要指定域名和服务器的详细信息。 利用acme.sh可以定期更新证书.
-
将SSL证书文件和私钥文件放置在服务器上:您需要将证书文件(通常是.crt格式)和私钥文件(通常是.key格式)放置在服务器上的安全位置。
-
配置Nginx以使用SSL:在Nginx的server块中,添加以下配置来启用SSL:
server { listen 443 ssl; server_name example.com; ssl_certificate /path/to/certificate.crt; ssl_certificate_key /path/to/private.key; ... }这将使Nginx监听HTTPS的443端口,并使用指定的证书和私钥文件进行SSL加密。
-
重新加载Nginx配置:在更改Nginx配置后,需要重新加载Nginx以使其生效。您可以使用以下命令重新加载Nginx:
sudo systemctl reload nginx
如果配置存在错误,则使用以下命令检查错误: sudo nginx -t 如果检查结果没有错误,可以重新加载Nginx。
可能的问题
-
服务器提供商使用了防火墙,使用`telnet host port`查看是否可以连接
-
大陆封禁了443端口的流量(可能)