常用命令
./nginx #run nginx windows下用start nginx
nginx -s stop #fast shutdown
nginx -s quit #graceful shutdown
#changing configuration, starting new worker processes with a new
#configuration, graceful shutdown of old worker processes
nginx -s reload
nginx -s reopen #re-opening log files
nginx.conf配置,搞定非智力的技术
#user nobody; 配置允许运行nginx服务的用户和用户组 eg: user nobogy nobody
worker_processes 3; # 配置允许Nginx进程生成的worker process数
#error_log logs/error.log; # 配置nginx服务运行中生成的日志的存放路径
#error_log logs/error.log notice; # 配置日志存放路径,满足该日志级别notice的日志会记录到该日志文件
#error_log logs/error.log info; # 日志级别 info
#pid logs/nginx.pid; # nginx进程文件路径,默认
events {
worker_connections 1024; # 配置最大连接数
}
http {
include mime.types; # 定义MIME-Type
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; # 配置允许使用sendfile方式传输
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; # 配置连接超时时间
#gzip on; # 是否开始gzip 压缩, 对于开放大json数据,建议开启压缩
# 配置虚拟主机 1
server {
# 配置监听的 域名和端口
listen 8081;
server_name localhost; # 配置成服务器的域名
# 定义字符集 utf-8
charset utf-8;
#access_log logs/host.access.log main;
# 访问日志 路径,格式, 可以用绝对路径
access_log myweb/server1/log/access.log main;
# 配置访问路径 http://myServer1:8081/server1/location1的根目录,首页
location /server1/location1 {
root myweb; # 不是/开头的为相对路径
index index.html index.htm;
}
location /server1/location2 {
root myweb; # 不是/开头的为相对路径
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;
# 访问路径 http://myServer1:8081/50x.html, 到 root目录去找文件
location = /50x.html {
root myweb;
}
location = /404.html {
root myweb;
}
# 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;
#}
}
# 配置虚拟主机 2
server {
listen 8082;
server_name 127.0.0.1;
charset utf-8;
#access_log logs/host.access.log main;
# 访问日志 路径,格式, 可以用绝对路径
access_log myweb/server2/log/access.log main;
# 配置访问路径 http://myServer1:8081/server1/location1的根目录,首页
location /server2/location1 {
root myweb; # 不是/开头的为相对路径
index index.html index.htm;
}
location /svr2/loc2 {
alias myweb/server2/location2/; #对location的URI进行更改
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;
# 访问路径 http://myServer1:8081/50x.html, 到 root目录去找文件
location = /50x.html {
root myweb;
}
location = /404.html {
root myweb;
}
# 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;
# }
#}
}
目录结构如下:这里用的是相对目录,在nginx的安装目录下新建一个myweb目录。
访问测试