步骤:
一:安装window (我目前使用的系统是win7)
官网下载地址: https://nginx.org/en/download.html 如下图点击windows下载解压:
解压之后的文件目录:
二:运行nginx
在当前目录中打开cmd,快捷键方式:ctrl+shift -->鼠标右键---》在此处打开命令窗口
启动nginx: start nginx
在浏览地址栏输入:localhost 就会启动nginx默认html页面 || 在cmd输入ipconfig获取当前使用电脑的ip地址
如上图启动成功;
三:配置自己个服务端口及代理
D:\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 {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root F:\test; #访问文件根目录
index index.html index.htm; #访问文件首页
}
location /api { #代理api前缀
proxy_pass https://www.baidu.com/; #代理服务器
}
}
#这里是我自己配置服务端口
# 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;
# }
#}
}
根据上图配置之后,在浏览器中访问:http://localhost:81 就会显示相关的页面,凡是以 /api 前缀发起的请求,都将请求代理到:https://www.baidu.com/
常用到的命令如下:
start nginx :启动nginx
nginx -s stop :快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。
nginx -s quit :平稳关闭Nginx,保存相关信息,有安排的结束web服务。
nginx -s reload :因改变了Nginx相关配置,需要重新加载配置而重载。
nginx -s reopen :重新打开日志文件。
nginx -c filename :为 Nginx 指定一个配置文件,来代替缺省的。
nginx -t :不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
nginx -v:显示 nginx 的版本。
nginx -V:显示 nginx 的版本,编译器版本和配置参数。