首先说明一下版本,我下载的是 nginx/1.14.0 (Ubuntu)
我们都知道 —— 当我们将nginx下载成功后,nginx就默认启动了应用,默认为80端口 —— 为什么呢?我们从nginx的默认配置中一探究竟。
注意看我的注释,# 开头的都是注释,其中**中文的# 开头的是我写的注释**,其他的为nginx默认配置的注释。
当你只是当 _静态web服务器 _这种最基础的使用的话,把我中文标注的内容看懂了就够用**~以后我也会写更多文章分享“更深入”的配置理解(包括性能优化)。
nginx.conf 文件
# 指定运行用户,可以为rootuser www-data;# 指定子进程数worker_processes auto;# 指定运行时的pid的存储位置pid /run/nginx.pid;# 指定接受 /etc/nginx/modules-enabled/ 该路径下的所有.conf结尾的配置# 里面的文件都默认为空include /etc/nginx/modules-enabled/*.conf;# 工作模式设置events {# 指定最大并发数worker_connections 768;# multi_accept on;}# http配置设置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;# 指定/etc/nginx/mime.types文件设定mime类型include /etc/nginx/mime.types;# 指定默认处理的文件类型为二进制文件default_type application/octet-stream;### SSL证书设置# 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压缩设置# 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### 指定接受 /etc/nginx/conf.d/ 该路径下的所有.conf结尾的配置,默认里面为空# 可以将自己的nginx子服务配置统一放到这里管理include /etc/nginx/conf.d/*.conf;# 指定接受 /etc/nginx/sites-enabled/下的所以内容# 路径内有一个default文件,里面是80端口的server设置# 下面会单独介绍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;# }#}
/etc/nginx/sites-enabled/* 下配置介绍(只有一个文件 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.### 默认的server配置# Default server configuration#server {# 监听80端口listen 80 default_server;listen [::]:80 default_server;# SSL configuration## 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;# 当前server服务的web文件的根目录root /home/my-blog/public;# 如果你使用PHP,那么在下面index内再加入index.php# Add index.php to the list if you are using PHP# 访问服务的默认首页文件(顺序依次排开)index index.html index.htm index.nginx-debian.html;# 指定server名称,可以为ip/域名,多个配置之间用空格隔开server_name 59.110.70.85;# 当前server的路由配置location / {# First attempt to serve request as file, then# as directory, then fall back to displaying a 404.# 顺序检查文件# 首次访问是将其当做一个文件查找($uri),# 如果没有的话再当做文件夹($uri/,有个斜杠代表文件夹),# 还没有的话返回404try_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;#}}# 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 简单理解和配置
