Linux - Nginx

Linux - Nginx

Tim 36 2024-10-10

搬运之前的笔记,记不清具体细节,实际遇到了再完善吧

安装

sudo apt-get install -y nginx

配置

配置文件存放位置: /etc/nginx

添加站点配置

# 站点配置存放目录
cd /etc/nginx/sites-available
# 添加一个站点配置
vim test.conf
  • HTTP配置

#负载均衡器
upstream webapi{
	server 192.168.0.4:5555;
	#5555后面加上/ 则最后转发地址去掉/api/,不加则最后加上/api/ (ip:port/api/...)
}
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /home/tim/website/vue/test/;
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
		try_files $uri $uri/ index.html;
	}

	location /api/{
		#proxy_pass http://192.168.0.4:5555/;
		proxy_pass http://webapi/;
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection keep-alive;
       	proxy_set_header Host $http_host;# $host获取的是请求的IP $http_host获取的是请求的IP+端口
        proxy_cache_bypass $http_upgrade;			
	}
}
  • HTTPS配置

upstream webapi{
        server 192.168.0.4:5555;
        #5555后面加上/ 则最后转发地址去掉/api/,不加则最后加上/api/ (ip:port/api/...)
}

#自定义变量
geo $sslport {
        default 433;

}

#81重定向到443
server {
 listen 81;
 #请填写绑定证书的域名
 server_name timoxo.cn home.timoxo.cn;
 #把http的域名请求转成https
 return 301 https://$host:$sslport$request_uri;
}

# 配置443端口
server {
         #SSL 默认访问端口号为 443
         listen 433 ssl http2;

         #请填写绑定证书的域名
         server_name timoxo.cn;

       # if ($scheme != "https") {
       #       return 301 https://$host:433$request_uri;
       #}

        #请填写证书文件的相对路径或绝对路径
         ssl_certificate  /daily/ssl/timoxo.cn_bundle.crt;
         #请填写私钥文件的相对路径或绝对路径
         ssl_certificate_key /daily/ssl/timoxo.cn.key;
         ssl_session_timeout 5m;
         #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
         ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
         #请按照以下协议配置
         ssl_protocols TLSv1.2 TLSv1.3;
         ssl_prefer_server_ciphers on;

        root /daily/web/website/vue/testweb/;
        index index.html index.htm index.nginx-debian.html;

        location / {
                #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
                #例如,您的网站主页在 Nginx 服务器的 /etc/www 目录下,则请修改 root 后面的 html 为 /etc/www。
                #root /data/website/vue/testweb/;
                #index index.html index.htm;
                try_files $uri $uri/ index.html;

        }

         location /api/{
                #proxy_pass http://192.168.0.4:5555/;
                proxy_pass http://webapi/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection keep-alive;
                proxy_set_header Host $http_host;
                # $host获取的是请求的IP $http_host获取的是请求的IP+端口
                proxy_cache_bypass $http_upgrade;
        }

}

# 配置443端口
server {
         #SSL 默认访问端口号为 443
         listen 433 ssl http2;

         #请填写绑定证书的域名
         server_name home.timoxo.cn;

        #请填写证书文件的相对路径或绝对路径
         ssl_certificate  /daily/ssl/home.timoxo.cn_bundle.crt;
         #请填写私钥文件的相对路径或绝对路径
         ssl_certificate_key /daily/ssl/home.timoxo.cn.key;
         ssl_session_timeout 5m;
         #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
         ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
         #请按照以下协议配置
         ssl_protocols TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;

        root /daily/web/website/vue/testweb/;
        index index.html index.htm index.nginx-debian.html;

        location / {
                #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
                #例如,您的网站主页在 Nginx 服务器的 /etc/www 目录下,则请修改 root 后面的 html 为 /etc/www。
                #root /data/website/vue/testweb/;
                #index index.html index.htm;
                try_files $uri $uri/ index.html;

        }

         location /api/{
                #proxy_pass http://192.168.0.4:5555/;
                proxy_pass http://webapi/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection keep-alive;
                proxy_set_header Host $http_host;
                # $host获取的是请求的IP $http_host获取的是请求的IP+端口
                proxy_cache_bypass $http_upgrade;
        }

}

验证站点配置

nginx -t

启用站点配置

需要在/etc/nginx/sites-enabled目录中创建符号链接

cd /etc/nginx/sites-enabled  

ln -s /etc/nginx/sites-available/test.conf test.conf

清理日志

可以创建一个定时任务清理Nginx日志:

http://blog.timoxo.cn/archives/cWJ8E8Cn