启动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