在Linux上部署nginx,有两种方法:
这两种方法安装的nginx,在配置文件的内容上有一些区别,本文主要概述这些差异。
这一章介绍的是通过压缩包编译、安装所部署的nginx的配置文件结构,安装过程不再赘述
nginx版本:nginx-1.18.0.tar.gz
.
├── client_body_temp
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf # 核心配置文件
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp
├── html # 放置静态资源
│ ├── 50x.html
│ └── index.html
├── logs # 放置日志文件
│ ├── access.log
│ ├── error.log
│ └── nginx.pid
├── proxy_temp
├── sbin
│ └── nginx
├── scgi_temp
└── uwsgi_temp
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
可以发现:
用户想要部署自己的项目,就在这个配置文件中写就可以了,新增server配置即可
这一章介绍的是通过docker容器部署的nginx的配置文件结构,安装过程不再赘述
nginx版本:nginx:latest
docker容器默认将nginx安装在:/etc/nginx/
路径下
/etc/nginx/
├── conf.d
│ ├── default.conf
├── fastcgi_params
├── mime.types
├── modules
├── nginx.conf # 核心配置文件
├── scgi_params
├── uwsgi_params
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
可以发现:
var/log/nginx/
路径下include /etc/nginx/conf.d/*.conf;
根据目录结构,去conf.d文件夹下面,看看default.conf的内容
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
可以看到:
nginx.conf
文件中定义好了分析这一行引用:include /etc/nginx/conf.d/*.conf;
可知:
conf.d
文件夹下面,所有以.conf
结尾的文件都会被引用到nginx.conf
主配置文件里面conf.d
文件夹下面新增一个xxx.conf
文件,写入server配置即可目录结构
准备一个路径:/xxx/xxxx
.
├── conf # 用于放置配置文件
│ ├── conf.d
│ │ ├── default.conf ### 从容器里面copy下来的
│ │ ├── icwp-django.conf ### 自定义的
│ │ └── icwp-vue.conf ### 自定义的
│ └── nginx.conf ### 从容器里面copy下来的
├── html # 用于放置静态资源
│ ├── 50x.html
│ ├── index.html
├── log # 用于挂载日志
若是要对默认配置文件做改动,先启动一个容器,将里面需要的配置文件拉出来即可,然后将这个容器删除
# 运行nginx容器
docker run -it -d --name=test_nginx -p 80:80 nginx
# 拷贝文件到当前路径下
docker cp test_nginx:/etc/nginx/nginx.conf nginx.conf
docker cp test_nginx:/etc/nginx/conf.d/default.conf default.conf
docker cp test_nginx:/usr/share/nginx/html/index.html index.html
docker cp test_nginx:/usr/share/nginx/html/50x.html 50x.html
# 删除容器
docker rm -f test_nginx
docker启动nginx
docker run -d \
-p 80:80 \
--name nginx-test \
-v ./conf/nginx.conf:/etc/nginx/nginx.conf \ # 挂载主配置文件
-v ./conf/conf.d:/etc/nginx/conf.d \ # 挂载自定义的配置文件
-v ./log:/var/log/nginx \ # 挂载日志文件
-v ./html:/usr/share/nginx/html \ # 挂载静态资源文件
nginx:latest