Linux安装Nginx及HTTPS反向代理配置详解

如果你是使用的若依框架,且服务器使用的CenterOS8,那么恭喜你可以直接通过该文章直接安装启动即可,100%可执行!!!

1、工欲善其事必先利其器,下载安装包

离线安装

去官网下载Nginx对应的安装包,如下图所示,请下载stable版本包或者根据自己的需要选择下载历史版本的包,下载地址:nginx: download

在线安装
wget http://nginx.org/download/nginx-1.22.1.tar.gz

wget https://www.openssl.org/source/openssl-3.0.8.tar.gz

wget http://zlib.net/zlib-1.2.13.tar.gz

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.45.tar.gz

# 抑或是通过以下命令直接安装,那么请忽略第三步对应的安装包即可

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2、安装C++编译环境

yum install gcc-c++

3、安装各种环境包

# 进入到已经下载的安装包目录
# openssl 安装

tar zxvf openssl-3.0.8.tar.gz
cd openssl-3.0.8
./config && make && make install

# pcre安装

tar zxvf pcre-8.45.tar.gz
cd pcre-8.45
./configure && make && make install

# zlib安装

tar zxvf zlib-1.2.13.tar.gz
cd zlib-1.2.13
./configure && make && make install

# nginx安装

tar zxvf nginx-1.22.1.tar.gz
cd nginx-1.22.1
./configure --with-http_ssl_module && make && make install

4、进入Nginx安装目录

# 进入Nginx安装后的目录
whereis nginx
# 一般会在以下目录,如果不是请根据上面的命令cd进去
cd /usr/local/nginx

5、配置Nginx文件

vim /usr/local/nginx/conf/nginx.conf

# 参考以下配置文件即可

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    # HTTP server
    server {
        listen       80;
        server_name  www.domain.com domain.com;
        return 301 https://$host$request_uri;
        # rewrite ^/(.*)$ https://$host$1 permanent;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        # location / {
        #     root   html;
        #     index  index.html index.htm;
        # }

        # 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;
        #}
        
        #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   html;
        # }

    }

    # HTTPS server
    server {
       listen       443 ssl;
       server_name  www.domain.com domain.com;

       ssl_certificate      /ssl/domain/domain.com.pem;
       ssl_certificate_key  /ssl/domain/domain.com.key;

       ssl_session_cache    shared:SSL:1m;
       ssl_session_timeout  5m;

       ssl_ciphers  HIGH:!aNULL:!MD5:!DES:!3DES;
       ssl_prefer_server_ciphers  on;
       ssl_protocols TLSv1.2;

       add_header X-Frame-Options DENY;
       add_header X-XSS-Protection "1; mode=block";
       add_header X-Content-Type-Options "nosniff";
       add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

       server_tokens off;
       etag off;

       error_page  405 =200 $uri;

       location / {
        #    root   html;
           root       /usr/local/nginx/html/;
           index      index.html index.htm;
           try_files  $uri $uri/ /index.html;
           error_page 405 =200 @405;
       }
       location @405 {
           proxy_set_header Host $http_host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header REMOTE-HOST $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_pass       http://localhost:8080$request_uri;
       }
       location /ip {
           proxy_pass            https://ip.cn/api/;
           proxy_ssl_server_name on;
       }

       location /prod-api {
           rewrite ^/prod-api/?(-*)$ /$1 break;

           proxy_pass http://127.0.0.1:8080/;
           proxy_redirect off;
           proxy_set_header Host $http_host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header REMOTE-HOST $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
           proxy_set_header X-Forwarded-Port $server_port;

           proxy_buffer_size 64k;
           proxy_buffers   32 32k;
           proxy_busy_buffers_size 128k;

           gzip on;
           gzip_min_length 1k;
           gzip_buffers 4 16k;
           gzip_comp_level 5;
           gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png  video/mp4;

           if ($request_filename ~* .*.(html|htm)$) {
               add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
           }
           if ($request_filename ~* .*.(gif|jpg|jpeg|png|bmp|swf|ico|pdf|psd|pdd|mp4)$) {
                expires 30d;
           }
           if ($request_filename ~ .*.(js|css)$) {
               expires 1d;
           }
       }
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

6、Java项目用Maven打包之后用一下命令启动

nohup java -jar ruoyi-admin.jar --server.port=8080 &

7、Nginx启动

cd /usr/local/nginx/sbin

# 启动
./nginx
# 停止/重启 (quit、reload)
./nginx -s stop
# 命令帮助
./nginx -h
# 验证配置文件
./nginx -t

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值