ubuntu系统,nginx的安装和配置。
安装nginx.
apt install nginx-core
查看nginx的安装位置。
whereis nginx
回显信息。
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
修改nginx的配置文件
查看nginx的版本信息
nginx -V
回显
nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure arguments: --with-cc-opt=‘-g -O2 -ffile-prefix-map=/build/nginx-d8gVax/nginx-1.18.0=. -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2’ --with-ld-opt=‘-Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -Wl,-z,now -fPIC’ --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --add-dynamic-module=/build/nginx-d8gVax/nginx-1.18.0/debian/modules/http-geoip2 --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module
配置文件–conf-path=/etc/nginx/nginx.conf
cd /etc/nginx
ls
vi nginx.conf
- 配置文件的结构
配置文件由简单指令和块指令构成。每一条简单指令由名称和参数组成,名称和参数中间用至少一个空格分开,参数结束后要写分号(;
)。块指令由名称和附加的结构组成,附加的结构就是用大括号({}
)包裹的一些简单指令或附加结构。每一行的#
号后面的内容都是注释。
- 配置文件的指令
简单指令有user,worker_processes,pid,include,include代表可以包含其它配置文件。块指令有events,http,mail,server,location,其中events,http,mail是跟简单指令平级的。http和mail块指令可以包含server块指令和location块指令。如果不提供mail服务,可以将整个mail块指令通过#注释。
- 具体配置
/etc/nginx/nginx.conf
user root; #用户名需要改为启动nignx服务的用户,否则可能报403错误
worker_processes auto;
pid /run/nginx.pid;
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;
server {
listen 443 ssl; # 有ssl证书写443 ssl,没有证书写80或注释此行
server_name xxx.top; # 写域名或IP地址或localhost
ssl_certificate cert/xxx.top_server.crt; # 有ssl证书写路径,没有就注释掉
ssl_certificate_key cert/xxx.top_server.key; # 有ssl证书写路径,没有就注释掉
# cert目录位于/etc/nginx
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://127.0.0.1:8000; # nginx作为代理服务器,将所有请求转到http://127.0.0.1:8000,app应用要在http://127.0.0.1:8000此地址运行
}
location /static/ {
root /root/app/static # 静态文件服务,请求地址为/static/开头的,直接转到/root/app/static目录下,不需要app处理,直接返回。此块可以注释掉
}
}
##
# 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;
error_log /var/log/nginx/error.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/*;
}
# nginx作为邮箱服务器,不需要可以注释掉
#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;
# }
#}
其它配置参考http://nginx.org/en/docs/beginners_guide.html