转自http://www.xingximing.cn/article/ngconf
Nginx配置
-
创建vhost文件夹用于保存不同域名对应的 Nginx 配置文件
-
为什么这么做呢?主要是考虑到线上日后会有很多配置都在nginx的conf里,这样单个文件会显得很臃肿,所以这里我们采用分开配置,主体合并的方法,分支都放在vhost中
- vim nginx.conf 编辑 Nginx 配置文件 加上 include vhost/*.conf;在所示位置
- 在 vhost 文件夹里创建域名配置 vim 你的域名.conf
- 进行如图的配置
这里需要说明的是 :
listen 80 表示监听80端口
server_name 表示请求的url
proxy_pass http://localhost:8080 表示转发来自80端口的请求到8080端口
重启Nginx
sbin/nginx -s reload
具体配置
server {
default_type 'text/html';
charset utf-8;
# 监听80端口
listen 80;
autoindex on;
# 定义使用www.xingximing.com访问(这个地方可以配置多个)
server_name www.xingximing.cn;
# 设定本虚拟主机的访问日志
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp index.php;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
# 对 "/" 启用反向代理
location / {
# 域名转发
proxy_pass http://127.0.0.1:8080;
add_header Access-Control-Allow-Origin *;
}
}