nginx官方文档说明:http://nginx.org/en/linux_packages.html#RHEL-CentOS
一、安装前准备:
yum install yum-utils
二、添加源
到 cd /etc/yum.repos.d/ 目录下
新建 vim 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
[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
三、安装Nginx
通过yum search nginx看看是否已经添加源成功。如果成功则执行下列命令安装nginx。
yum install nginx
安装完后,rpm -qa | grep nginx 查看
启动nginx:systemctl start nginx
加入开机启动:systemctl enable nginx
查看nginx的状态:systemctl status nginx
在浏览器输入自己服务器的IP地址即可访问到nginx,如下图所示,nginx服务的默认端口为80(这里需要注意防火墙的限制和端口冲突)。
用命令lsof -i:80,可查看80端口被那个进程占用。
nginx服务的默认配置文件在 vim /etc/nginx/conf.d/default.conf ,打开可看到,默认端口为80,项目部署目录为/usr/share/nginx/html/。
往 /usr/share/nginx/html/ 目录下上传一个JavaScript写的飞机大战。
在浏览器里输入http://192.168.0.146/Plane/test/,即可访问到。
四、https和前端一起的配置
worker_processes auto;
error_log /var/log/nginx/error.log;
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
client_header_buffer_size 32k;
large_client_header_buffers 4 128k;
client_header_timeout 3600s;
client_body_timeout 3600s;
client_max_body_size 500m;
fastcgi_read_timeout 3000;
fastcgi_connect_timeout 3000;
fastcgi_send_timeout 3000;
fastcgi_buffers 2 256k;
fastcgi_buffer_size 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 32k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 64k;
#include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name admin.kelibeibi.xyz;
root /usr/local/nginx/dist;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
upstream kelibeibi {
server 127.0.0.1:8080;
}
server {
listen 80;
listen [::]:80;
server_name www.kelibeibi.xyz;
root /usr/local/nginx/html;
location / {
proxy_pass http://kelibeibi;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# HTTPS server
server {
listen 443 ssl;
server_name www.kelibeibi.xyz;
ssl_certificate /etc/nginx/cert/6699162_www.kelibeibi.xyz.pem;
ssl_certificate_key /etc/nginx/cert/6699162_www.kelibeibi.xyz.key;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:1m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://kelibeibi;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}