nginx新手入门教程
一、 安装
1、编译安装
下载安装包nginx-1.16.1.tar.gz
使用命令tar -xf nginx-1.16.1.tar.gz
进入解压后的目录执行以下命令
./configure --prefix=/opt/nginx --with-http_ssl_module --with- http_stub_status_module
make && make install
--prefix=/opt/nginx 编译过程中指定安装目录
--with-http_ssl_module 启用https支持
--with- http_stub_status_module 静态客户端状态
make && make install 编译安装nginx
2、通过yum安装
执行 yum install -y nginx即可安装
二、目录结构
nginx安装完成后有如下目录
conf 存放配置文件
html 存放静态文件
sbin 存入nginx执行程序,二进制文件
logs 存放日志文件
nginx启动之后会生成一个主进程,根据配置文件的选项来生成子进程,也叫工作进程。主进程不负责处理用户的请求,用来转发用户的请求给子进程处理
三、 配置
1、配置文件
点击查看代码
#user nobody; 指定哪个用户来启动子进程
worker_processes 1;工作进程的个数,一般配置为cpu的核心数-1或-2
#cpu的亲缘性绑定,让nginx的子进程工作在哪个核心上
#error_log logs/error.log; 日志存放目录
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; 启动后会生成pid文件
events {
#理论并发值=worker_processes*worker_connections
#每一颗cpu的工作连接数
#use[epoll|select|poll]默认select;
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;#指定默认的index页面
}
#错误页面找不到页面
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#错误页面500服务端错误
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;
# }
#}
}
2、404页面配置
error_page /404.html;
3、root与alias的区别
location /img{
root /data/img;
}
root: /data/img里必须有/img
alias:/data/img里不需要有/img
4、域名配置
server-name 配置域名如baidu.com;
多域名访问配置如下
server {
listen 80 default_server;#通过ip访问默认访问此服务
server_name jd.com www.jd.com;
location / {
root /data/jd;
index index.html;
}
}
server {
listen 80;
server_name taobao.com www.taobao.com;
location / {
root /data/taobao;
index index.html;
}
}

浙公网安备 33010602011771号