Vagrant中Nginx配置
2015-10-24 15:57
886 查看
在系列文章1和系列文章2文章中,介绍了Vagrant的安装和开发环境软件的自动安装。
这篇文章将写点关于虚拟机中Nginx的配置,以及在真实机中访问Nginx的方法。
打开Vagrantfile文件中,找到如下配置:
config.vm.network "forwarded_port", guest: 80, host: 8080
该配置的意思就是将虚拟机的80端口映射到真实机的8080端口。
使用
vagrant ssh命令进入虚拟机
备份默认nginx配置文件
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.back
修改配置
打开/etc/nginx/nginx.conf,将里面的内容更改如下:
user www-data; worker_processes 4; pid /var/run/nginx.pid; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; ## # nginx-naxsi config ## # Uncomment it if you installed nginx-naxsi ## #include /etc/nginx/naxsi_core.rules; ## # nginx-passenger config ## # Uncomment it if you installed nginx-passenger ## #passenger_root /usr; #passenger_ruby /usr/bin/ruby; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; include /vagrant/files/nginx/conf.d/*.conf; server { listen 80; server_name lanffy.com www.lanffy.com; charset utf-8; index index.html index.htm; root /projects/; } }
添加HTML页面
在虚拟机中:cd /projects
在该目录下新建index.html或者index.htm文件,内容如下:
<html> <head> <title>R_Lanffy</title> </head> <body> Hello World </body> </html>
重启Vagrant
vagrant reload
修改hosts
sudo vim /etc/hosts
添加:
127.0.0.1 www.lanffy.com
访问测试
在真实机浏览器中输入地址:lanffy.com:8080或者
www.lanffy.com:8080即可访问到虚拟机中的nginx相关配置。
如果想达到输入test.com就能访问的目的,是需要将Vagrantfile文件中的8080修改为80
注:如果出现不能访问的情况,很有可能是在启动虚拟机之前,8080端口被占用了。解决办法就是将端口修改为没有被占用的端口。
查看端口是否被监听:
netstat -an | grep 8080
相关文章推荐
- nginx代理指定目录
- 访问Nginx发生SSL connection error的一种情况
- Nginx+Naxsi部署专业级Web应用防火墙
- CentOS 6.2实战部署Nginx+MySQL+PHP
- nginx中http核心模块的配置指令2
- nginx中http核心模块的配置指令3
- nginx中http核心模块的配置指令4
- nginx中http的fastcgi模块的配置指令1
- Nginx 学习笔记(一)
- 网站502与504错误分析
- 艰难完成 nginx + puma 部署 rails 4的详细记录
- 把Lua编译进nginx步骤方法
- web 应用中常用的各种 cache详解
- Linux系统上配置Nginx+Ruby on Rails+MySQL超攻略
- window+nginx+php环境配置 附配置搭配说明
- 解析CI即CodeIgniter框架在Nginx下的重写规则
- 将PHP从5.3.28升级到5.3.29时Nginx出现502错误
- 基于Nginx0.8.54+PHP5.3.4+MySQL5.5.8的全新LNMP稳定版架构搭建的VPS
- Nginx(PHP/fastcgi)的PATH_INFO问题