Nginx基础
安装
Nginx官网
安装必备yum组件
sudo yum install yum-utils
要设置 yum 存储库,请创建具有以下内容的文件
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
默认情况下,使用稳定 nginx 包的存储库。 如果你想使用主线nginx包, 运行以下命令:
sudo yum-config-manager --enable nginx-mainline
安装 nginx
sudo yum install nginx
nginx目录
[root@localhost etc]# rpm -ql nginx
/etc/logrotate.d/nginx #日志轮转文件
/etc/nginx #Nginx 配置文件夹
/etc/nginx/nginx.conf #总配置文件
/etc/nginx/conf.d #Nginx 子配置文件夹
/etc/nginx/conf.d/default.conf #Nginx 默认子配置文件(默认网站)
/etc/nginx/fastcgi_params #动态网站模块文件-python,php所需的相关变量
/etc/nginx/scgi_params #动态网站模块文件-python所需的相关变量
/etc/nginx/uwsgi_params #动态网站模块文件-python所需的相关变量
/etc/nginx/mime.types #关联程序 不同的文件使用不同的程序处理
/etc/nginx/modules #模块文件夹,存储第三方模块
/usr/lib/systemd/system/nginx-debug.service #Nginx调试程序启动脚本
/usr/lib/systemd/system/nginx.service #Nginx服务脚本
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx #Nginx主程序
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.24.0 #Nginx共享文档
/usr/share/doc/nginx-1.24.0/COPYRIGHT #Nginx共享文档
/usr/share/man/man8/nginx.8.gz #Nginx共享文档
/usr/share/nginx #Nginx共享文档
/usr/share/nginx/html #Nginx共享文档
/usr/share/nginx/html/50x.html #Nginx共享文档
/usr/share/nginx/html/index.html #Nginx共享文档
/var/cache/nginx #Nginx缓存
/var/log/nginx #Nginx日志文件夹
配置信息
[root@localhost etc]# nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments:
--prefix=/etc/nginx #安装目录的路径
--sbin-path=/usr/sbin/nginx #可执行文件的路径
--modules-path=/usr/lib64/nginx/modules #模块文件的路径
--conf-path=/etc/nginx/nginx.conf #主配置文件的路径
--error-log-path=/var/log/nginx/error.log #错误日志文件的路径
--http-log-path=/var/log/nginx/access.log #访问日志文件的路径
--pid-path=/var/run/nginx.pid #进程 ID 文件的路径
--lock-path=/var/run/nginx.lock #锁文件的路径
--http-client-body-temp-path=/var/cache/nginx/client_temp #缓存文件的路径
--http-proxy-temp-path=/var/cache/nginx/proxy_temp #代理缓存文件的路径
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp #php缓存文件的路径
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp #python缓存文件的路径
--http-scgi-temp-path=/var/cache/nginx/scgi_temp #python缓存文件的路径
--user=nginx #运行 Nginx 进程的用户名
--group=nginx #运行 Nginx 进程的用户组
--with-compat #启动动态兼容性
--with-file-aio
--with-threads
--with-http_addition_module #多线程模块
--with-http_auth_request_module
--with-http_dav_module
--with-http_flv_module
--with-http_gunzip_module
--with-http_gzip_static_module #启用 Gzip 静态文件压缩模块
--with-http_mp4_module
--with-http_random_index_module
--with-http_realip_module #用 Real IP 模块,用于获取真实客户端IP
--with-http_secure_link_module #nginx安全下载模式
--with-http_slice_module #nginx中文文档
--with-http_ssl_module #启用 HTTP SSL 模块
--with-http_stub_status_module #启用状态模块,提供服务器运行状态和统计信息
--with-http_sub_module #nginx替换网站内容
--with-http_v2_module #启用 HTTP/2 模块
--with-mail
--with-mail_ssl_module
--with-stream #负载均衡1
--with-stream_realip_module #负载均衡2
--with-stream_ssl_module #负载均衡3
--with-stream_ssl_preread_module #负载均衡4
--with-pcre # PCRE 库支持,用于正则表达式匹配。
--with-openssl # OpenSSL 库支持,用于加密和安全通信
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
Nginx配置
文件结构
... #全局块
events { #events块
...
}
http #http块
{
... #http全局块
server #server块
{
... #server全局块
location [PATTERN] #location块
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局块
}
主配置(/etc/nginx/nginx.conf)
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; # 注意在这里引入子配置文件
}
子配置(/etc/nginx/conf.d/default.conf)
server {
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;
#}
}
配置汇总
@千锋linux云计算Nginx教程
@Nginx 中文官方文档
@张龙豪之Nginx配置详解