Nginx网站服务器
Nginx安装
Nginx Web服务器软件安装完成后,程序主目录位于/usr/local/nginx/,该目录下的内容分别为
目录 | 作用 |
---|---|
conf | 主配置文件目录 |
html | 网站根目录 |
logs | 日志文件目录 |
sbin | 主程序目录 |
Nginx启动与停止
进入sbin目录下(以下操作,如无特殊说明,均在sbin目录下操作)
启动nginx
./nginx :启动nginx,这种方式nginx会自动读取conf目录下的nginx.conf的配置文件
./nginx -c 指定配置文件:用指定的配置文件启动nginx
./nginx -s reopen :重新打开日志文件
[root@localhost sbin]# ./nginx
[root@localhost sbin]# ps -aux |grep nginx
root 831 0.0 0.0 25516 1352 ? Ss 14:50 0:00 nginx: master process ./nginx
nobody 832 0.0 0.0 28032 2124 ? S 14:50 0:00 nginx: worker process
root 834 0.0 0.0 112660 976 pts/2 S+ 14:51 0:00 grep --color=auto nginx
一个主进程
关闭nginx
./nginx -s stop :快速停止nginx
./nginx -s quit :完整有序的停止nginx
其他停止nginx方式:
1.通过信号量强行关闭
[root@localhost nginx]# ps -aux | grep nginx
root 831 0.0 0.0 25516 1352 ? Ss 14:50 0:00 nginx: master process ./nginx
nobody 832 0.0 0.0 28032 2368 ? S 14:50 0:00 nginx: worker process
root 954 0.0 0.0 112660 976 pts/2 S+ 14:59 0:00 grep --color=auto nginx
[root@localhost nginx]# kill -INT 831
[root@localhost nginx]# ps -aux | grep nginx
root 964 0.0 0.0 112660 972 pts/2 S+ 15:00 0:00 grep --color=auto nginx
2.改变配置文件,平滑重读配置文件
[root@localhost nginx]# ps -aux|grep nginx
root 985 0.0 0.0 25516 1984 ? Ss 15:01 0:00 nginx: master process ./sbin/nginx
nobody 1864 0.0 0.0 27600 2060 ? S 15:49 0:00 nginx: worker process
root 2005 0.0 0.0 112664 976 pts/2 S+ 15:52 0:00 grep --color=auto nginx
[root@localhost nginx]# kill -HUP 985
重启nginx
./nginx -s reload : 重启,一般修改配置后重新加载生效
Nginx配置文件
Nginx默认配置文件为/usr/local/nginx/conf/nginx.conf,配置文件主要包括全局,event,http,server设置。event主要用来定义Nginx工作模式;http提供web功能;server用来设置虚拟主机,且server必须位于http内部,一个配置文件中可以有多个server
... #全局块
events { #events块
...
}
http #http块
{
... #http全局块
server #server块
{
... #server全局块
location [PATTERN] #location块
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局块
}
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
//设置用户与组,一般用#注释掉
user nobody;
//启动子进程数,可以通过ps aux | grep nginx 查看
worker_processes 1;
//错误日志文件,以及日志级别
error_log logs/error.log info;
//进程号保存的文件
pid logs/nginx.pid;
//主要用来定义Nginx工作模式
events {
//每个进程可以处理的链接数,受系统文件句柄的限制
worker_connections 1024;
}
//提供web功能
http {
//mime.types为文件类型定义文件
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
//使用server定义虚拟主机
server {
listen 80;
server_name www.baidu.com;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
//消费者查询系统中间件 、 全程版系统中间件
location ~ /api/1.0/ll/(.*) {
proxy_pass http://59.68.29.89:18081/api/1.0/ll/$1;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}