下载地址
linux版本安装
进入解压后(nginx-source改名)文件的目录
./configure
make
make install
whereis nginx
cd /usr/local/nginx
进入sbin目录中执行
1.启动命令: ./nginx
2.重启命令: ./nginx -s reload
3.关闭命令: ./nginx -s stop
windows版本安装
Nginx 进程项说明
进程1: Nginx主进程 主要负责反向代理的服务. 占用内存大的
进程2: Nginx守护进程 主要负责保护主进程意外关闭的. 占用内存小的
如何手动的关闭: 先关闭守护进程 再关闭主进程
cmd命令
说明: 要求在nignx.exe所在的根目录中执行
启动命令: start nginx 如果nginx配置文件有错 不会打印报错信息
重启命令: nginx -s reload 如果nginx配置文件有错 会打印报错信息
关闭命令: nginx -s stop
nginx配置举例
配置文件在conf/nginx.conf
举例:
#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
server {
#nginx监听的端口号
listen 80;
#nginx要拦截的域名
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#表示nginx执行反向代理的具体动作
# /代表拦截所有请求
location / {
#root代表映射的是一个目录,目录名字为html
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{
listen 80;
server_name image.xx.com;
location / {
root D:/work/s_image;
}
}
#配置后台服务器
server{
listen 80;
server_name manager.xx.com;
location / {
proxy_pass http://localhost:8080;
#proxy_pass http://xxWindows;
}
}
#配置tomcat集群,轮询策略,依次访问;weight权重策略(等级);IPHASH(用户绑定,同一个人访问同一台服务器)
#iphash(hash(ip)%数量)缺点:由于hash计算可能出现负载不均衡
#如果某台服务器宏机,则直接影响绑定的用户
#一般hash可以作为压测使用,内部测试
#down 标识宏机的服务器
#backup 标识备用机,用户不会访问备用机,当主机遇忙或宏机才会访问
#max_fails=1 设定最大的失败次数->就规定fail_timeout=60s (60s)不再访问
upstream xxWindows {
#ip_hash;
server localhost:8080 weight=6 backup;
server localhost:8081 weight=3 down;
server localhost:8082 weight=1 down;
}
#配置前台服务器
server{
listen 80;
server_name www.xx.com;
location / {
proxy_pass http://localhost:8092;
}
}
server{
listen 80;
server_name sso.xx.com;
location / {
proxy_pass http://localhost:8093;
}
}
}