一、安装Nginx
1. 添加Nginx官方库
- 安装yum-utils
yum -y install yum-utils
- 配置nginx官方源
sudo vim /etc/yum.repos.d/nginx.repo
- 在文件中添加以下内容
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
- 如不熟悉vim编辑器操作,可手动在
/etc/yum.repos.d/
目录下创建nginx.repo
文件,打开文件后,在文件内添加以上文件内容,保存
2. 安装Nginx
sudo yum install nginx -y
- 安装完成后,可使用
nginx -v
查看版本 - 可使用
rpm -ql nginx
查看安装目录和其他文件位置
3. 配置Nginx
- 打开配置文件
sudo vim /etc/nginx/nginx.conf
- 默认配置文件
# /etc/nginx/nginx.conf
user nginx; #work进程用户(打工者),root是老板
worker_processes auto; #work进程数(几个打工者)
error_log /var/log/nginx/error.log notice; #错误日志文件路径
pid /var/run/nginx.pid;
events {
worker_connections 1024; #一个work可以处理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; #扩展配置(虚拟主机配置文件)
}
- 扩展配置文件,对于单个不同网站配置,可自行创建
# /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/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 /usr/share/nginx/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;
#}
}
server {
listen 888;
server_name my_project1;
location / {
root /usr/share/nginx/my_project1;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/my_project1/html;
}
}
server {
listen 8888;
server_name my_project2;
location / {
root /usr/share/nginx/my_project2;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/my_project2/html;
}
}
fastcgi_param
配置文件解释:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径
fastcgi_param QUERY_STRING $query_string; #请求的参数;如?app=123
fastcgi_param REQUEST_METHOD $request_method; #请求的动作(GET,POST)
fastcgi_param CONTENT_TYPE $content_type; #请求头中的Content-Type字段
fastcgi_param CONTENT_LENGTH $content_length; #请求头中的Content-length字段。
fastcgi_param SCRIPT_NAME $fastcgi_script_name; #脚本名称
fastcgi_param REQUEST_URI $request_uri; #请求的地址不带参数
fastcgi_param DOCUMENT_URI $document_uri; #与$uri相同。
fastcgi_param DOCUMENT_ROOT $document_root; #网站的根目录。在server配置中root指令中指定的值
fastcgi_param SERVER_PROTOCOL $server_protocol; #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
fastcgi_param GATEWAY_INTERFACE CGI/1.1;#cgi 版本
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;#nginx 版本号,可修改、隐藏
fastcgi_param REMOTE_ADDR $remote_addr; #客户端IP
fastcgi_param REMOTE_PORT $remote_port; #客户端端口
fastcgi_param SERVER_ADDR $server_addr; #服务器IP地址
fastcgi_param SERVER_PORT $server_port; #服务器端口
fastcgi_param SERVER_NAME $server_name; #服务器名,域名在server配置中指定的server_name
二、常见操作
- 启动
systemctl start nginx
- 停止
systemctl stop nginx
- 重启
systemctl restart nginx
- 设置开机启动
systemctl enable nginx