首页 > 其他 > 详细

nginx 使用配置知识

时间:2015-10-21 19:14:45      阅读:144      评论:0      收藏:0      [点我收藏+]
启动nginx:
service nginx start
启动后观察进程信息:
ps aux | grep nginx,得到结果:
root      3630  0.0  0.0   7892   684 ?        Ss   02:24   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     3631  0.0  0.1   8052  1484 ?        S    02:24   0:00 nginx: worker process                  
root      3647  1.0  0.0   4356   728 pts/1    S+   02:26   0:00 grep nginx
技术分享
从上面信息,我们看到nginx的启动命令实际为:
/usr/sbin/nginx -c /etc/nginx/nginx.conf
其中/usr/sbin/nginx是nginx程序 -c是选取配置文件 /etc/nginx/nginx.conf配置文件路径
打开文件/etc/nginx/nginx.conf:
vi /etc/nginx/nginx.conf, 我们看到:
user  nginx;
worker_processes  1;                                                                                                           

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

}
在http参数中,我们发现了一个特殊的词语:include,其含义是,除了本配置文件之外,还有一些配置信息写在了其他被“include”过的文件中,最重要的是这句: 
include /etc/nginx/conf.d/*.conf;
这句话的意思是/etc/nginx/conf.d/目录下面的所有以conf为后缀名的文件都被http因为配置项,我们要设置自己的配置,只需要在本目录下面创建一个后缀名为conf的文件,添加配置项。
于是我在该目录下创建了文件myserver.conf,其内容如下:
server {
     listen     80;   // 监听端口
     server_name  localhost; // 监听地址
     location / {
            root   /usr/share/nginx/html;   // 根目录位置
            index  index.html index.htm; // 作为默认页的页面文件
     }
 
}
 
配置好设置后,可以启动nginx,实验一把了
service nginx restart
技术分享
现在去浏览器上面访问服务器ip,结果:
技术分享
奇怪!
通过排查发现:权限问题,注意nginx.conf中有这样一句话:
user  nginx;
意思是,当前开启nginx程序的用户是nginx用户,而我的目录/usr/share/nginx/html属于root用户,显然权限不够,于是把上面那句话修改为:
user  root;
在root用户登录状态下执行:
service nginx restart
现在访问IP就一切正常了

nginx 使用配置知识

原文:http://www.cnblogs.com/candycloud/p/4898592.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!