- 安装 nginx;
- 配置 nginx.conf 文件
- 在根目录下找到 conf 文件夹下的 nginx.conf 文件,根据自己的需求做如下配置(含说明):
#user nobody; //nginx用户及组:用户组。windows下不指定
worker_processes 1; //工作进程:数目。
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
//错误日志:存放路径。
#pid logs/nginx.pid;
//pid(进程标识符):存放路劲
events {
worker_connections 1024;
//每个工作进程最大连接数量
}
###设定http服务器,利用它的反向代理功能提供负载均衡支持
//一个http里面可以配置多个server 。监听端口可以一样,只要域名和路径不一样就可以了
http {
include mime.types;//设定mime类型,类型由mime.type文件定义
default_type application/octet-stream;
##配置虚拟机
server {
listen 80;//监听端口
server_name testapp.zhlc.com; //访问域名
root E:/webstorm/zhlc_h5/debug/; //路径:自己的代码源文件目录
index index.html index.htm; //首页
location ~ \.(jpg|jpeg|png|gif|js|css|ico|txt|xml|swf|wav|eot|ttf|otf|woff|svg|woff2) { //对以jpg……等结尾的地址进行负载均衡
expires 30d;
}
}
server {
listen 8080;
server_name ngx-ha.***img.com;
location / {
stub_status on;
access_log off;
}
}
server {
listen 80;
server_name imgsrc1.***.net;
root html;
}
server {
listen 80;
server_name ***.com w.***.com;
# access_log /usr/local/nginx/logs/access_log main;
location / {
rewrite ^(.*)$ http://www.***.com/ ;
}
}
}
//纯配置代码:
#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;
server {
listen 80;
server_name testapp.zhlc.com;
root E:/webstorm/zhlc_h5/debug/;
index index.html index.htm;
location ~ \.(jpg|jpeg|png|gif|js|css|ico|txt|xml|swf|wav|eot|ttf|otf|woff|svg|woff2) {
expires 30d;
}
}
}
- 配置 host ,打开switchHosts,切换host:
#本机(回环地址) 域名
127.0.0.1 xxx.xxx.com
> start nginx //起服务
> nginx -s reload //重启服务
> nginx -s stop //停止服务