[笔记]Nginx使用及Windows/Linux部署

简介: [笔记]Nginx使用及Windows/Linux部署

Windows

使用方式

启动

nginx.exe

配置转发

端口转发

http 80->8080

进入conf

修改 nginx.conf

如果配置http的

找80端口 解开注释

server {
        listen       80;
        server_name  localhost;# 服务名
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass https://127.0.0.1:8080;# 转发地址 可以是ip+端口
        }

https 443->8080

进入conf

修改 nginx.conf

如果配置https的

找443端口 解开注释

# HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  localhost;
        ssl_certificate      D:\tools\ngnix-1.12.2\nginx-1.12.2\ssl_aliyun\5115589_www.shiver.fun.pfx;
        ssl_certificate_key  D:\tools\ngnix-1.12.2\nginx-1.12.2\ssl_aliyun\pfx-password.txt;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass https://book_pool;
        }

注意:

PFX证书是window下面的证书,所以你Linux证书需要使用这个证书,我们需要把.PFX证书转换一下。我这里就转换成crt格式的了。

ssl_certificate_key需要证书的公钥

ssl_certificate需要.cert文件

都需要openssl生成

Nginx如何使用.PFX证书

关于解决nginx ssl问题处理

地址转发

常用命令

ngnix -s reload 
ngnix -s stop # 常常没用 需要手动 tasklist 然后 taskkill /pid /f
ngnix  #启动

Linux

nginx安装教程

完毕后

/usr/local/nginx

sbin里面有编译好的nginx

conf配置

/usr/local/nginx/nginx-1.9.9

objs里面有编译好的nginx

conf配置

记Nginx跨域问题

https://segmentfault.com/a/1190000020179829?utm_source=tag-newest

https://www.jianshu.com/p/520021853827

nginx方案

https://blog.csdn.net/zanpengfei/article/details/86605837

失败

https://cloud.tencent.com/developer/article/1648860

失败

后端方案

package team.shiver.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import team.shiver.handler.AuthenticationInterceptor;
@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor())
                .addPathPatterns("/**");    // 拦截所有请求,通过判断是否有 @LoginRequired 注解 决定是否需要登录
    }
    @Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }
    //跨域处理
    @Override
    public void addCorsMappings(CorsRegistry corsRegistry){
        /**
         * 所有请求都允许跨域,使用这种配置就不需要
         * 在interceptor中配置header了
         */
        corsRegistry.addMapping("/**").
                allowedOrigins("*"). //允许跨域的域名,可以用*表示允许任何域名使用
                allowedMethods("*"). //允许任何方法(post、get等)
                allowedHeaders("*"). //允许任何请求头
                allowCredentials(true). //带上cookie信息
                exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L); //maxAge(3600)表明在3600秒内,不需要再发送预检验请求,可以缓存该结果
    }
}

可行

Nginx使用场景

1.代理转发

常见问题

1.访问网站某些资源网站提示 net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK)

原因

查看nginx 错误日记 tail -f nginx/logs/error.log,显示 nginx/proxy_temp/ 目录 无权限。

解决方案

chmod -R 777 nginx/proxy_temp/
相关文章
|
5天前
|
Ubuntu 前端开发 JavaScript
技术笔记:Ubuntu:一个部署好的tomcat应用(war包)怎么用Nginx实现动静分离?
技术笔记:Ubuntu:一个部署好的tomcat应用(war包)怎么用Nginx实现动静分离?
|
5天前
|
Linux API 调度
技术笔记:Linux内核跟踪和性能分析
技术笔记:Linux内核跟踪和性能分析
|
5天前
|
算法 Linux 编译器
技术笔记:LINUX2.6.32下的进程分析
技术笔记:LINUX2.6.32下的进程分析
|
4天前
|
Unix Shell Linux
技术笔记:linux中SIGHUP与nohup的关系
技术笔记:linux中SIGHUP与nohup的关系
|
4天前
|
Unix 关系型数据库 Linux
技术笔记:linux学习心得
技术笔记:linux学习心得
10 0
|
4天前
|
Linux 数据安全/隐私保护 Windows
pscp 将Linux服务器上的文件同步到Windows服务器上
【6月更文挑战第28天】pscp 将Linux服务器上的文件同步到Windows服务器上
11 0
|
4天前
|
机器学习/深度学习 Unix Java
技术笔记:Linux之Shell脚本编程(一)
技术笔记:Linux之Shell脚本编程(一)
|
4天前
|
缓存 负载均衡 应用服务中间件
技术笔记:Nginx配置详解
技术笔记:Nginx配置详解
|
4天前
|
网络协议 算法 Linux
技术笔记:Linux学习:TCP粘包问题
技术笔记:Linux学习:TCP粘包问题
|
4天前
|
网络协议 Linux Shell
技术笔记:Linux中的两种守护进程standalone和xinetd
技术笔记:Linux中的两种守护进程standalone和xinetd