Nginx基础
一、nginx启动、重启、加载等
要启动nginx,请运行可执行文件。 当nginx启动后,可以通过使用-s参数调用可执行文件来控制它。 使用以下语法:
nginx -s signal
信号(signal)的值可能是以下之一:
stop - 快速关闭服务
quit - 正常关闭服务
reload - 重新加载配置文件
reopen - 重新打开日志文件
例如,要通过等待工作进程完成服务当前请求来停止nginx进程,可以执行以下命令:
nginx -s quit
在将重新配置命令的命令发送到nginx或重新启动之前,配置文件中的更改将不会被应用。 要重新加载配置文件,请执行:
nginx -s reload
在linux下查询nginx相关进程:
ps -ax | grep nginx
二、提供静态内容服务
nginx -v 查看版本
nginx -V 查看安装目录、配置目录等信息
nginx配置文件一般在/etc/local/nginx下,为nginx.conf文件,需要去除conf.d里的default文件
开启建议静态内容服务提供:
1.创建内容文件夹
# mkdir -p /data/www
# mkdir -p /data/images
2.放入内容物
一个重要的Web服务器任务是提供文件(如图像或静态HTML页面)。根据请求,
文件将从不同的本地目录提供:/data/www(可能包含HTML文件)和/ data/images(包含图像)。
这将需要编辑配置文件,并使用两个位置块在http块内设置服务器块。
分别在上面创建的两个目录中放入两个文件:/data/www/index.html 和 /data/images/logo.png,/data/www/index.html文件的内容就一行,如下 :
<h2> New Static WebSite Demo.</h2>
3.配置文件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;
server{
location / {
root /data/www;
}
location /images/ {
root /data;
}
location ~ \.php$ {
root /data/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
主要修改处为
location / {
root /data/www;
}
location /images/ {
root /data;
}
如果文件不存在,nginx将发送一个指示404错误的响应。
网址访问若不加/images路径,如192.168.211.101/images/logo.png则访问不到png文件,
三、代理服务器简单搭建
配置一个基本的代理服务器,它为来自本地目录的文件提供图像请求,并将所有其他请求发送到代理的服务器
添加代理的文件路径目录:
/data/up1
并且添加文件demo.html
写入:
<h2>About proxy_pass Page at port 8080</h2>
配置文件:
## 新服务(服务处理)
server {
listen 8080;
root /data/up1;
location / {
}
}
## 代理配置,数据转发
server {
location / {
proxy_pass http://localhost:8080/;
}
location ~ \.(gif|jpg|png)$ {
root /data/images;
}
再执行:
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

浙公网安备 33010602011771号