1.下载windows版的nginx
下载地址:http://nginx.org/en/download.html
2.解压与启动
解压后的目录,直接双击nginx.exe即可启动
dos命令:
注:nginx解压路径最好不要有空格
启动nginx: start nginx
停止nginx: nginx -s stop
重新加载nginx: nginx -s reload
查看nginx启动情况: tasklist /fi “imagename eq nginx.exe”
强行关闭nginx: taskkill /F /IM nginx.exe
3.nginx配置
#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 {
#use epoll; #使用epoll的I/O 模型。linux建议epoll,window下不指定
worker_connections 1024; #这是指 一个子进程最大允许连1024个连接
keepalive_timeout 60; #keepalive超时时间
}
http { #配置http服务器的主要段
include mime.types; #设定mime类型,类型由mime.type文件定义
default_type application/octet-stream;
#该段为自定义log输出样式函数。配合下面的access_log使用
#日志格式设置:
#$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
#$remote_user:用来记录客户端用户名称;
#$time_local: 用来记录访问时间与时区;
#$request: 用来记录请求的url与http协议;
#$status: 用来记录请求状态;成功是200,
#$body_bytes_sent :记录发送给客户端文件主体内容大小;
#$http_referer:用来记录从那个页面链接访问过来的;
#$http_user_agent:记录客户浏览器的相关信息
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#开启log输出。输出位置为logs/access.log(相对于nginx安装根路径) 使用自定的哪个输出格式
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#是否开启压缩
#gzip on;
server { #这是虚拟主机段
listen 800; #监控端口
server_name localhost; #监控域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / { #定位,个人理解就是java中的filter
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 www.lyric.com;
location / {
root html;
index index.html index.htm;
}
location /test {
proxy_pass http://proxy_test
}
}
upstream proxy_test { #定义负载均衡设备的Ip及设备状态
server 192.168.127.134:8080/test; #默认 轮询
server 192.168.127.135:8080/test weight 10; #指定轮询几率 数值越大,几率越大
}
# HTTPS请求反向代理
#
server {
listen 443 ssl; #监听拦截443端口
server_name localhost; #监听的域名地址
ssl_certificate D:/BaiduYunDownload/ssl/server.crt; #此处路径中不能有空格 证书
ssl_certificate_key D:/BaiduYunDownload/ssl/server.key; #秘钥
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / { #其他规则都不匹配时,走这个
proxy_pass http://localhost:8080/;
}
location /v2/ {
proxy_store off;
proxy_redirect off; # 关闭重定向
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Referer 'no-referrer-when-downgrade';
proxy_set_header User-Agent 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36';
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_pass https://api.douban.com/v2/; #反向代理方法
}
error_page 404 /404.html;
location = /40x.html {}
error_page 500 502 503 504 /50x.html;
location = /50x.html {}
}
}
这里主要为了配置https服务的反向代理,所以,在这之前,需要先生成证书.
4.生成证书
此处详细参考:https://www.cnblogs.com/vincent-li666/p/5851463.html
(1).首先要安装Openssl
下载地址:http://slproweb.com/products/Win32OpenSSL.html
(2).然后设置环境变量
%OPENSSL_HOME%/bin;
(3).创建私钥
在命令行中执行命令: openssl genrsa -des3 -out server.key 1024
这里需要输入设置密码,后面会用到
(4).创建csr证书
在命令行中执行命令: openssl req -new -key server.key -out server.csr
输入的信息中最重要的为 Common Name,这里输入的域名即为我们要使用https访问的域名
(5).去除密码
在命令行执行命令:
copy server.key server.key.org
openssl rsa -in server.key.org -out server.key
输入之前设置的密码
(6).生成crt证书
在命令行中执行此命令: openssl x509 -req -days 365 -in lee.csr -signkey lee.key -out lee.crt
到此,当前目录下生成四个文件,将crt证书和key密钥添加到nginx.conf配置中
重新启动nginx
注:有启动失败情况,查看是否端口被占用
主要参考:https://blog.csdn.net/u014459326/article/details/53364849