安装
1:添加Nginx存储库
sudo yum -y install epel-release2.安装nginx
sudo yum -y install nginx3.启动Nginx
sudo systemctl start nginx4.如果想在系统启动时启用Nginx,也可以不设置
sudo systemctl enable nginx5.设置防火墙
允许http通信
sudo firewall-cmd --permanent --zone=public --add-service=http
允许https通信
sudo firewall-cmd --permanent --zone=public --add-service=https
开发80 443
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
6.重启防火墙
sudo firewall-cmd --reload
注(如果你是在阿里云买的服务器,还要进入控制台开放80 和443端口,不然还是访问不了)
测试
在浏览器输入你服务器的ip出现下图,说明成功了
域名配置
我是在阿里云买的域名,5块一年,用来测试还挺好(这个域名要在阿里云上配置,映射到服务器的ip)
进入
cd /etc/nginx
ls
下图所有文件都会自动生成,处理cert文件夹,是自己创建的用于放ssl证书的(下面会说怎么配置ssl)
一般服务器都是80 22 443这个三个端口开放,我这里的配置也是一样,其他的端口请求都是通过80,和443端口来转发。
先修改nginx.conf 如下
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
# 开启gzip
gzip on;
# 启用gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 256k;
# gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明
gzip_comp_level 6;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
include /usr/local/nginx/conf/include/*;
}
进入conf.d文件夹
创建一文件 xxx.conf
没有ssl证书的配置如下 这个是通过代理方式把4080端口弄到80端口上,这样4080端口就不对外开放,也访问不了。
在服务器中请求接口 比如是===> ip:4080/user
在浏览器 要写成===> 域名/test/user 就可以访问了
多个端口同理 多增加upstream,和下面名字对应的location 就可以了,这样你就不用购买多个域名了
upstream test{
server 127.0.0.1:4080;
keepalive 64;
}
upstream test2{
server 127.0.0.1:4081;
keepalive 64;
}
server {
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
listen 80;
server_name 域名;
location /output/ {
alias /data/www/html/;
index false;
autoindex on;
}
location /test/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://test/;
}
location /test2/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://test2/;
}
error_page 404 /404.json;
location = /404.json {
default_type application/json;
root /usr/share/nginx/html/404.json;
}
error_page 500 502 503 504 /50x.json;
location = /50x.json {
default_type application/json;
root /usr/share/nginx/html;
}
}
当你有ssl证书时(可以在阿里云上购买一个免费的)
购买流程 点击你购买的域名
在点击ssl证书
直接申请 选择个人免费版
买好之后进入ssl证书管理
审核过了,点击下载,下载nginx版本的证书,解压会有两个文件,一个.key和.pem,把这两个文件放到/etc/nginx/cert下
现在来说ssl 和nginx怎么配置 这个和上面那个配置有冲突,请选择一种
进入conf.d文件夹
创建一文件 xxx.conf
upstream test{
server 127.0.0.1:4080;
keepalive 64;
}
server {
listen 80;
server_name 域名;
#http请求指向https
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name 域名;
ssl_certificate "/etc/nginx/cert/.pem文件";
ssl_certificate_key "/etc/nginx/cert/.key文件";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /output/ {
alias /data/www/html/;
index false;
autoindex on;
}
location /test/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://test/;
}
error_page 404 /404.json;
location = /404.json {
default_type application/json;
root /usr/share/nginx/html/404.json;
}
error_page 500 502 503 504 /50x.json;
location = /50x.json {
default_type application/json;
root /usr/share/nginx/html;
}
}
重启nginx
nginx -s reload
查看nginx配置是否有问题
nginx -t
检查nginx是否启动
ps -ef | grep nginx
查询80端口是否绑定nginx
netstat -ntlp
停止nginx
nginx pkill -9 nginx