下载官网:http://nginx.org/
如果使用https,则先获取ssl,两个文件,key与crt文件,这里使用openssl创建:
1、创建key:
openssl genrsa -des3 -out ss.key 1024
2、创建csr:
openssl req -new -key ss.key -out ss.csr
3、清除key密码:
openssl rsa -in ss.key -out sss.key
4、创建crt:
openssl x509 -req -days 365 -in ss.csr -signkey sss.key -out ss.crt
nginx指令:
1、启动:start nginx(推荐) 或者 nginx.exe
2、停止:nginx.exe -s stop 或者 nginx.exe -s quit
3、重启:nginx.exe -s reload
4、查看版本:nginx -v
配置 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;
access_log off;
sendfile on;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;#长连接超时时间,单位是秒
#gzip on;#启用Gizp压缩
upstream myserver{
server 127.0.0.1:8080 weight=1;#weight值越大,请求越多
#server 127.0.0.1:5000 weight=2;#server 127.0.0.1:5000 weight=2 backup;备用机机制,即当其它机器宕机或者繁忙无响应后启用
}
upstream myserver2{
#server 127.0.0.1:8080 weight=1;#weight值越大,请求越多
server 127.0.0.1:5000 weight=2;#server 127.0.0.1:5000 weight=2 backup;备用机机制,即当其它机器宕机或者繁忙无响应后启用
}
server {
listen 80;#拦截的端口
server_name localhost;#拦截的域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://myserver;#代理的服务器
proxy_redirect default;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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 key/ss.crt;
ssl_certificate_key key/sss.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;
proxy_pass http://myserver;#代理的服务器
proxy_redirect off;
}
}
server {
listen 80;#拦截的端口
server_name www.x-ys.com;#拦截的域名
# 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;
proxy_pass http://myserver2;#代理的服务器
proxy_redirect default;
}
}
}
如果发现启动错误,或者无法启动,查看日志:/logs/error.log