mac下nginx服务器设置虚拟主机

发布 : 2016-07-07 分类 : IT 浏览 :

编辑如下

1
2
3
4
127.0.0.1 localhost
127.0.0.1 myblog.com //设置你想要用的域名
255.255.255.255 broadcasthost
::1 localhost

设置的域名甚至可以是线上已有的域名,如baidu.com,不过这样子设置之后就不能访问线上的真实网站了

启动nginx端口监听

  1. 进入nginx目录,如果你使用homebrew安装的nginx的话,那么目录是/usr/local/etc/nginx
1
cd /usr/local/etc/nginx
  1. 查看配置文件nginx.conf
    确认最后一行有下面这句,没有的话就手动添加
1
include servers/*;
  1. 打开nginx目录下的文件夹servers(如无,手动添加),新建一个server,命名为blog
1
2
cd servers
vim blog

编辑如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
server {
listen 8080; //此处修改端口号
server_name myblog.com; //此处添加hosts中设置的域名
root /Users/jim/blog; //此处设置项目根目录
location ~ \.php$ { ## Execute PHP scripts
expires off; ## Do not cache dynamic content
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
location / {
index index.html index.php; ## Allow a static html file to be shown first
try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
expires 30d; ## Assume all files are cachable
if ($request_uri ~* "\.(png|gif|jpg|jpeg|css|js|swf|ico|txt|xml|bmp|pdf|doc|docx|ppt|pptx|zip)$") {
expires max;
}
# set fastcgi settings, not allowed in the "if" block
include /usr/local/etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$; #this line
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param SCRIPT_NAME /index.php;
fastcgi_param MAGE_RUN_CODE default;
fastcgi_param MAGE_RUN_TYPE store;
# rewrite - if file not found, pass it to the backend
if (!-f $request_filename) {
fastcgi_pass 127.0.0.1:9000;
break;
}
}
}

然后就可以用你设置的域名和端口号(此处为jim.practise.com:8080)来访问你的项目了
若监听的是80端口那么域名后面的端口号可以省略

以后如果要使用别的域名,只需要在servers文件夹下面继续新增就可以了

本文作者 : 小凡
原文链接 : https://16bh.github.io/2016/07/07/set-nginx-server/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
留下足迹