若依前端 配置https 进行nginx反向代理配置

先把 ssl 证书的 nginx配置 pem文件和key文件 复制到 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;

    server {
        listen       端口;    # http监听端口
        server_name  域名;  #你的域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        client_max_body_size 1024m; # 使用 Nginx 服务器上传文件时上传大小 

        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;   #这一句就是防刷新404或500
	      rewrite ^(.*)$ https://${server_name}$1 permanent;#Http跳转Https请求
        }


        gzip on;
	  gzip_static on;
	  gzip_min_length 1k;
 	  gzip_comp_level 9;
 	  gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/    gif image/png;
 	  gzip_vary on;
 	  gzip_disable "MSIE [1-6]\.";

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

        # 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;
        #}
		location /api/ { #/api 是代理前缀
			# 你的后台地址
			proxy_pass http://127.0.0.1:9878/;  
			proxy_set_header Host $http_host;
			client_max_body_size    20m;
			client_body_buffer_size 1280k;
			proxy_connect_timeout 15s;
			proxy_send_timeout 15s;
			proxy_read_timeout 15s;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		}
    }


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


    # HTTPS server
    #
    server {
        listen       端口 ssl;  #https监听端口
        server_name  域名;  # 证书域名

        ssl_certificate      pem文件路径;   # 域名nginx证书
        ssl_certificate_key  key文件路径; # 域名nginx证书密钥

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        location / {
		try_files $uri $uri/ @rewrites;    
	  }
        location @rewrites {
	        rewrite ^(.+)$ /index.html last;
	  }



        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers  on;
	  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

        location /api/ {
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header Host $http_host;
			proxy_pass http://127.0.0.1:9878/;   #后台地址
        }
    }

}

重启nginx 服务即可

posted @ 2022-06-28 10:20  HikL[爱心]  阅读(2467)  评论(0)    收藏  举报