Nginx基础
概述
安装 nginx
卸载 nginx
nginx 基本命令
查看 nginx 路径
查看软件安装路径
查看运行文件所在路径
程序在运行时查看路径
whereis nginx
which nginx
ps -ef | grep nginx
检查配置文件是否正确
nginx -t
启动 nginx
./nginx
启动 nginx 服务
sudo systemctl start nginx
快速停止 nginx
nginx -s stop
停止 nginx 服务
sudo systemctl stop nginx
正常停止 nginx
nginx -s quit
查看 nginx 服务状态
sudo systemctl status nginx
修改配置文件 nginx.conf 后,重新加载
nginx -s reload
nginx 配置文件
概述
- nginx.conf 是 nginx 的配置文件, 配置文件由多个指令块组成, 每个指令块包含一组指令, 用于定义服务器的行为.
- Nginx 配置文件主要指令块:
全局块, events 块, http 块, server 块, location 块.
- 全局块: 影响 Nginx 全局(如工作进程数、用户等).
- events 块: 影响 Nginx 服务器与客户端网络连接.
- http 块: 包含 HTTP 服务器的配置.
- server 块
- location 块: 定义如何处理特定的请求路径.
- ubuntu 下 nginx 的默认端口配置在 nginx/sites-available/default 中.
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.
# 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 84 default_server;
listen [::]:84 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:/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;
# }
#}
nginx通过 IP+端口 访问不同的静态资源
nginx.conf
conf.d/file_server.conf
conf.d/win7blog.conf
conf.d/video.conf
# 全局块
user www-data; # 指定运行 Nginx 的用户和组.
worker_processes auto; # 指定工作进程的数量,auto: 自动根据 CPU 核心数调整.
pid /run/nginx.pid;
error_log /var/log/nginx/error.log; # 指定错误日志的路径.
include /etc/nginx/modules-enabled/*.conf;
load_module /etc/nginx/modules/ngx_http_fancyindex_module.so;
# events 块
events {
worker_connections 768; # 指定每个工作进程可以同时处理的最大连接数.
# multi_accept on;
}
# http 块
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
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 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
##
# Gzip Settings
##
gzip on;
# 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; # 加载指定目录下所有以 .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;
# }
#}
# 使用 apt 安装的 nginx,添加 fancyindex 模块的方法
sudo apt-get install nginx-extras
# 下载 Nginx-Fancyindex-Theme 美化包
wget https://github.com/aperezdc/ngx-fancyindex
# 编辑 fileserver.conf 文件
# server 块
server {
listen 85;
server_name localhost;
charset utf-8,gbk;
root /DATA; # 网站根目录
# location 块
location / {
fancyindex on;
fancyindex_localtime on;
fancyindex_exact_size off;
fancyindex_header "/Nginx-Fancyindex-Theme-dark/header.html";
fancyindex_footer "/Nginx-Fancyindex-Theme-dark/footer.html";
fancyindex_ignore "examplefile.html"; # Ignored files will not show up in the directory listing, but will still be public.
fancyindex_ignore "Nginx-Fancyindex-Theme-dark"; # Making sure folder where files are don't show up in the listing.
# Warning: if you use an old version of ngx-fancyindex, comment the last line if you
# encounter a bug. See https://github.com/Naereen/Nginx-Fancyindex-Theme/issues/10
fancyindex_name_length 255; # Maximum file name length in bytes, change as you like.
}
}
# server 块
server {
listen 88;
server_name localhost;
location / {
#将该路径替换为您的网站根目录。
root /DATA/win7blog/public/; # root:指定请求路径对应的文件系统路径.
#添加默认首页信息
index index.html index.htm; # index:指定默认的索引文件.
}
}
server {
listen 9004;
server_name localhost;
charset utf-8,gbk;
location / {
#将该路径替换为您的网站根目录。
root /DATA/Media/; # root:指定请求路径对应的文件系统路径.
}
}
nginx通过二级域名访问不同的 web 服务
域名解析
nginx.conf
1. 配置两条解析记录:
1.1 在Cloudflare中将 test1.yhjedward.com 解析到服务器IP地址.
1.2 在Cloudflare中将 test2.yhjedward.com 解析到服务器IP地址.
user www-data;
worker_processes auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
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 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
##
# Gzip Settings
##
gzip on;
# 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/*;
server {
listen 80;
server_name test1.yhjedward.com;
location / {
proxy_pass http://127.0.0.1:5000;
}
}
server {
listen 80;
server_name test2.yhjedward.com;
location / {
proxy_pass http://127.0.0.1:8079;
}
}
}
#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官方文档: 1