nginx – Lwxyz https://yun.lwxyz.cn Don't Forget To Be Awesome! Fri, 16 Jun 2023 22:20:57 +0000 zh-CN hourly 1 https://wordpress.org/?v=5.1.13 Nginx配置跨域请求 Access-Control-Allow-Origin * https://yun.lwxyz.cn/2018/06/20/nginx%e9%85%8d%e7%bd%ae%e8%b7%a8%e5%9f%9f%e8%af%b7%e6%b1%82-access-control-allow-origin/ https://yun.lwxyz.cn/2018/06/20/nginx%e9%85%8d%e7%bd%ae%e8%b7%a8%e5%9f%9f%e8%af%b7%e6%b1%82-access-control-allow-origin/#respond Wed, 20 Jun 2018 01:59:22 +0000 http://blog.450823756.xyz/?p=134 阅读更多]]> 转自:http://blog.51cto.com/13523664/2060430

当出现403跨域错误的时候 No 'Access-Control-Allow-Origin' header is present on the requested resource,需要给Nginx服务器配置响应的header参数:

一、 解决方案

只需要在Nginx的配置文件中配置以下参数:

location / {  
  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
  add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
} 

上面配置代码即可解决问题了,不想深入研究的,看到这里就可以啦=-=

二、 解释

1. Access-Control-Allow-Origin

服务器默认是不被允许跨域的。给Nginx服务器配置Access-Control-Allow-Origin *后,表示服务器可以接受所有的请求源(Origin),即接受所有跨域的请求。

2. Access-Control-Allow-Headers 是为了防止出现以下错误:

Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

这个错误表示当前请求Content-Type的值不被支持。其实是我们发起了”application/json”的类型请求导致的。这里涉及到一个概念:预检请求(preflight request),请看下面”预检请求”的介绍。

3. Access-Control-Allow-Methods 是为了防止出现以下错误:

Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

发送”预检请求”时,需要用到方法 OPTIONS ,所以服务器需要允许该方法。

三、 预检请求(preflight request)

其实上面的配置涉及到了一个W3C标准:CROS,全称是跨域资源共享 (Cross-origin resource sharing),它的提出就是为了解决跨域请求的。

跨域资源共享(CORS)标准新增了一组 HTTP 首部字段,允许服务器声明哪些源站有权限访问哪些资源。另外,规范要求,对那些可能对服务器数据产生副作用的HTTP 请求方法(特别是 GET 以外的 HTTP 请求,或者搭配某些 MIME 类型的 POST 请求),浏览器必须首先使用 OPTIONS 方法发起一个预检请求(preflight request),从而获知服务端是否允许该跨域请求。服务器确认允许之后,才发起实际的 HTTP 请求。在预检请求的返回中,服务器端也可以通知客户端,是否需要携带身份凭证(包括 Cookies 和 HTTP 认证相关数据)。

其实Content-Type字段的类型为application/json的请求就是上面所说的搭配某些 MIME 类型的 POST 请求,CORS规定,Content-Type不属于以下MIME类型的,都属于预检请求:

application/x-www-form-urlencoded
multipart/form-data
text/plain

所以 application/json的请求 会在正式通信之前,增加一次”预检”请求,这次”预检”请求会带上头部信息 Access-Control-Request-Headers: Content-Type

OPTIONS /api/test HTTP/1.1
Origin: http://foo.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: Content-Type
... 省略了一些

服务器回应时,返回的头部信息如果不包含Access-Control-Request-Headers: Content-Type则表示不接受非默认的的Content-Type。即出现以下错误:

Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

]]>
https://yun.lwxyz.cn/2018/06/20/nginx%e9%85%8d%e7%bd%ae%e8%b7%a8%e5%9f%9f%e8%af%b7%e6%b1%82-access-control-allow-origin/feed/ 0
CentOS 7 下安装 Nginx https://yun.lwxyz.cn/2018/03/08/centos-7-%e4%b8%8b%e5%ae%89%e8%a3%85-nginx/ https://yun.lwxyz.cn/2018/03/08/centos-7-%e4%b8%8b%e5%ae%89%e8%a3%85-nginx/#respond Thu, 08 Mar 2018 05:54:51 +0000 http://blog.450823756.xyz/?p=77 阅读更多]]> 转自:https://www.linuxidc.com/Linux/2016-09/134907.htm

安装所需环境

Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境。

一. gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install gcc-c++

二. PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

yum install -y pcre pcre-devel

三. zlib 安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

四. OpenSSL 安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

官网下载

1.直接下载.tar.gz安装包,地址:https://nginx.org/en/download.html

nginx.png

2.使用wget命令下载(推荐)。

wget -c https://nginx.org/download/nginx-1.10.1.tar.gz

nginx-wget.png

我下载的是1.10.1版本,这个是目前的稳定版。

解压

依然是直接命令:

tar -zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1

配置

其实在 nginx-1.10.1 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。
1.使用默认配置

./configure

2.自定义配置(不推荐)

./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

编译安装

make
make install

查找安装路径:

whereis nginx

nginx-whereis.png

启动、停止nginx

cd /usr/local/nginx/sbin/
./nginx 
./nginx -s stop
./nginx -s quit
./nginx -s reload

./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

查询nginx进程:

ps aux|grep nginx

重启 nginx

1.先停止再启动(推荐):
对 nginx 进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。如下:

./nginx -s quit
./nginx

2.重新加载配置文件:
当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效,如下:
./nginx -s reload

启动成功后,在浏览器可以看到这样的页面:

nginx-welcome.png

开机自启动

即在rc.local增加启动代码就可以了。

vi /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx
设置执行权限:

chmod 755 rc.local

nginx-rclocal.png

到这里,nginx就安装完毕了,启动、停止、重启操作也都完成了,当然,你也可以添加为系统服务,我这里就不在演示了。

]]>
https://yun.lwxyz.cn/2018/03/08/centos-7-%e4%b8%8b%e5%ae%89%e8%a3%85-nginx/feed/ 0
nginx使用ssl模块配置支持HTTPS访问 https://yun.lwxyz.cn/2018/03/08/nginx%e4%bd%bf%e7%94%a8ssl%e6%a8%a1%e5%9d%97%e9%85%8d%e7%bd%ae%e6%94%af%e6%8c%81https%e8%ae%bf%e9%97%ae/ https://yun.lwxyz.cn/2018/03/08/nginx%e4%bd%bf%e7%94%a8ssl%e6%a8%a1%e5%9d%97%e9%85%8d%e7%bd%ae%e6%94%af%e6%8c%81https%e8%ae%bf%e9%97%ae/#respond Thu, 08 Mar 2018 05:53:12 +0000 http://blog.450823756.xyz/?p=74 阅读更多]]> 转自:https://www.cnblogs.com/saneri/p/5391821.html

默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译nginx时指定–with-http_ssl_module参数.

需求:
做一个网站域名为 www.localhost.cn 要求通过https://www.localhost.cn进行访问.

10.10.100.8 www.localhost.cn

实验步骤:

1.首先确保机器上安装了openssl和openssl-devel

1
2
#yum install openssl
#yum install openssl-devel

2.创建服务器私钥,命令会让你输入一个口令:

1
2
openssl genrsa -des3 -out server.key 1024  //生成私钥
#因为以后要给nginx使用.每次reload nginx配置时候都要你验证这个PAM密码的.由于生成时候必须输入密码,你可以输入后 再删掉。

3.创建签名请求的证书(CSR):

1
openssl req -new -key server.key -out server.csr                                //生成证书颁发机构,用于颁发公钥

4.在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:

1
2
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key                                  //除去密码以便reload询问时不需要密码

5.配置nginx
最后标记证书使用上述私钥和CSR:

1
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

6.修改Nginx配置文件,让其包含新标记的证书和私钥:

1
2
3
4
5
#vim /usr/local/nginx/conf/nginx.conf
http {
        include server/*.cn;
}

7.修改Nginx配置文件,让其包含新标记的证书和私钥:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#vim /usr/local/nginx/server/www.localhost.cn
server {
        listen       443;                                                                       //监听端口为443
        server_name  www.localhost.cn;
 
        ssl                  on;                  //开启ssl
        ssl_certificate      /etc/pki/tls/certs/server.crt;      //证书位置
        ssl_certificate_key  /etc/pki/tls/certs/server.key;      //私钥位置
        ssl_session_timeout  5m;
        ssl_protocols  SSLv2 SSLv3 TLSv1;            //指定密码为openssl支持的格式
        ssl_ciphers  HIGH:!aNULL:!MD5;              //密码加密方式
        ssl_prefer_server_ciphers   on;             //依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码
 
        location / {
            root   html;                        //根目录的相对位置
            index  index.html index.htm;
        }
    }

8.启动nginx服务器.

1
#/usr/local/nginx/sbin/nginx -s reload //如果环境允许的话直接杀掉进程在启动nginx

如果出现“[emerg] 10464#0: unknown directive “ssl” in /usr/local/nginx-0.6.32/conf/nginx.conf:74”则说明没有将ssl模块编译进nginx,在configure的时候加上“–with-http_ssl_module”即可

1
如:[root@localhost nginx-1.4.4]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module

9.测试网站是否能够通过https访问

1
https://www.localhost.cn

另外还可以加入如下代码实现80端口重定向到443

1
2
3
4
5
6
server {
listen 80;
server_name www.localhost.cn;
#rewrite ^(.*) https://$server_name$1 permanent;
rewrite ^(.*)$  https://$host$1 permanent;
}

过以下配置,可以设置一个虚拟主机同时支持HTTP和HTTPS

1
2
listen 80;
listen 443 default ssl;

同时支持80和443同时访问配置:

1
2
3
4
5
6
7
8
9
10
11
12
server {
    listen      80 default backlog=2048;
    listen      443 ssl;
    server_name  www.localhost.com;
   
    #ssl on;  //注释掉
    ssl_certificate   /usr/local/https/www.localhost.com.crt;
    ssl_certificate_key  /usr/local/https/www.localhost.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
    ssl_prefer_server_ciphers on;

Nginx 设置忽略favicon.ico文件的404错误日志(关闭favicon.ico不存在时记录日志)

在 server { … }内添加如下信息.

1
2
3
4
location = /favicon.ico {
log_not_found off;
access_log off;
}
]]>
https://yun.lwxyz.cn/2018/03/08/nginx%e4%bd%bf%e7%94%a8ssl%e6%a8%a1%e5%9d%97%e9%85%8d%e7%bd%ae%e6%94%af%e6%8c%81https%e8%ae%bf%e9%97%ae/feed/ 0
nginx:[emerg]unknown directive “ssl” https://yun.lwxyz.cn/2018/03/08/nginxemergunknown-directive-ssl/ https://yun.lwxyz.cn/2018/03/08/nginxemergunknown-directive-ssl/#respond Thu, 08 Mar 2018 05:51:51 +0000 http://blog.450823756.xyz/?p=72 阅读更多]]> 转自:http://blog.csdn.net/u014227715/article/details/77649156

在centos中,配置nginx的https时,出现如下错误。

nginx: [emerg] unknown directive “ssl” in /usr/local/nginx/conf/nginx.conf:102

到解压的nginx目录下

./configure –with-http_ssl_module

当执行上面语句,出现./configure: error: SSL modules require the OpenSSL library.

用 yum -y install openssl openssl-devel

再执行./configure

重新执行./configure –with-http_ssl_module

make ,切记不能make install 会覆盖。

把原来nginx备份

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

把新的nginx覆盖旧的

cp objs/nginx /usr/local/nginx/sbin/nginx

出现错误时cp: cannot create regular file ‘/usr/local/nginx/sbin/nginx’: Text file busy

用cp -rfp objs/nginx /usr/local/nginx/sbin/nginx解决

测试nginx是否正确

/usr/local/nginx/sbin/nginx -t

(nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful)

重启nginx

/usr/local/nginx/sbin/nginx -s reload

]]>
https://yun.lwxyz.cn/2018/03/08/nginxemergunknown-directive-ssl/feed/ 0