首页 > 系统相关 >## nginx 使用

## nginx 使用

时间:2022-09-29 21:39:19浏览次数:80  
标签:set http header ## nginx proxy 使用 root

nginx 使用

一、概述

有一个域名,有一台服务器,有多个服务。
为了可以通过域名来访问 服务器上的不同服务,而不将端口 对外 暴露。 采用 nginx 来进行 转发。

二、nginx 安装

不同的操作系统上,需要安装不同的包。
这里以在 centos6.5 上安装为例。
在 ​​​官网下载​​​ 最新的稳定包。 官网提供的包 需要自行进行 编译。
我想要将第三方包放在 当前我指定的目录下,比如 ~/share

Nginx 安装步骤

系统平台:CentOS release 6.6 (Final) 64位。

  • 1 安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
  • 2 要安装 PCRE
    PCRE 作用是让 Nginx 支持 Rewrite 功能。
# 1、下载 PCRE 安装包
[root@master ~]# cd ~/share
$ wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

# 2、解压安装包:
[root@master share]# tar zxvf pcre-8.35.tar.gz

# 3、进入安装包目录
[root@master share]# cd pcre-8.35

# 4、编译安装
[root@master pcre-8.35]# ./configure
[root@master pcre-8.35]# make && make install

# 5、查看pcre版本
[root@master pcre-8.35]# pcre-config --version
  • 3 安装 Nginx
# 1、下载 Nginx,下载地址:http://nginx.org/download/nginx-1.16.1.tar.gz
[root@master share]# cd ~/share
[root@master share]# wget http://nginx.org/download/nginx-1.16.1.tar.gz

# 2、解压安装包
[root@master share]# tar zxvf nginx-1.16.1.tar.gz

# 3、进入安装包目录
[root@master share]# cd 1.16.1

# 4、编译安装
[root@master nginx-1.16.1]# ./configure --prefix=/root/share/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/root/share/pcre-8.35
[root@master nginx-1.16.1]# make
[root@master nginx-1.16.1]# make install

# 5、查看nginx版本
[root@bogon nginx-1.16.1]# /root/share/nginx/sbin/nginx -v
# 到此,nginx安装完成。

你将会发现 产生了一个 /root/share/nginx 目录,这就是编译后的nginx 运行包,如果你还有其他同类机器需要安装 nginx,你只需要将此文件夹,移植到其他机器即可。
注意 需要设置 环境变量

  • 4 编写启停脚本

为了运行nginx 的方便,可以来编写 nginx.sh 脚本来控制它的启动停止
为了nginx 能自动重启,可以配置 nginx.service 放在 ​​​/lib/systemd/system/​​ 目录下

# nignx.service 的内容
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/root/share/nginx/sbin/nginx.sh start
ExecReload=/root/share/nginx/sbin/nginx.sh restart
ExecStop=/root/share/nginx/sbin/nginx.sh stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

详情参考:​​http://www.seekl.net/​

三、nginx 配置

1. Nginx同一个域名配置多个项目

使用nginx 在同一域名在 配置多个项目 有两种方式:

  1. nginx 按不同的目录发给不同的项目
  2. 启用 二级域名,不同的项目分片不同的域名
2.1 nginx 按不同的目录发给不同的项目
server {
listen 80;
server_name example.com;

location ^~ /project1 {
proxy_pass http://localhost:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location ^~ /project2 {
proxy_pass http://localhost:8082;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

这里配置了三个项目:

​http://example.com/project1路径分发到http://localhost:8081​​​​http://example.com/project2路径分发到http://localhost:8082​​ 其他路径分发到http://localhost:8080

2.2 启用 二级域名,不同的项目分片不同的域名
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# project1

server {
listen 80;
server_name project1.example.com;
location / {
proxy_pass http://localhost:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# project2

server {
listen 80;
server_name project2.example.com;
location / {
proxy_pass http://localhost:8082;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
注意:这三个项目属于不同的域名,项目之间通过http访问会存在跨域问题。
2.3 编写实际配置

根据我们的项目实际情况分析,应 采用 不同域名来访问

# 编写 nginx.conf 的配置,将其中引入多个配置文件
include vhost/*.conf;

四、nginx 常用命令

nginx -t   # 校验配置文件的正确性
nginx # 启动nginx
nginx -s reload # 重新载入配置文件
nginx -s reopen # 重启 Nginx
nginx -s stop # 停止 Nginx

五、nginx 安全

5.1 nginx 的跨域访问

当浏览器发起ajax请求到其他域名时,会出现跨域的问题,在nginx上的解决方案是配置Access-Control-Allow-Origin来解决,此参数只允许配置单个域名或者*,当我们需要允许多个域名跨域访问时却不好配置,可以用map来实现

map $http_origin $corsHost {
default 0;
"~http://blog.panpanie.com" http://blog.panpanie.com;
"~http://admin.panpanie.com" http://doc.panpanie.com;
"~http://www.panpanie.com" http://www.panpanie.com;
}
server
{
listen 80;
server_name search.panpanie.com;
root /nginx;
location /
{
add_header Access-Control-Allow-Origin $corsHost;
}
}

5.2 打开日志发现报错Permission denied

  1. 启动用户与nginx 工作用户不一致
[root@master nginx]# ps -ef | grep nginx
root 25604 1 0 17:32 ? 00:00:00 nginx: master process /root/share/nginx/sbin/nginx -p /root/share/nginx -c ./conf/nginx.conf
nobody 25605 25604 0 17:32 ? 00:00:00 nginx: worker process
root 25743 7036 0 17:32 pts/1 00:00:00 grep --color=auto nginx
  1. 修改 conf/nginx.conf 的user改为和启动用户一致
# vi conf/nginx.conf
user root
# 保存 :x

5.3 nignx 配置 valid_referer

为了配置一定的安全策略,将不允许 来自某些 域名的访问
valid_referers none blocked server_names *.panpanie.com;
if ($invalid_referer) {
rewrite ^/ ​​​http://www.panpanie.com/​​​ redirect ;
}


作者:​​​panie​​​


标签:set,http,header,##,nginx,proxy,使用,root
From: https://blog.51cto.com/u_15812342/5724043

相关文章

  • MarkDown 使用说明示例
    一、标题一级标题二级标题三级标题四级标题五级标题六级标题一级标题这是H2这是H3一级和二级标题还有一种写法就是下面加横杆,同时超过2个的=和-都可以有效果。This......
  • Spring 管理数据源
    Spring管理数据源 不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的。在以往的应用中,数据源一般是Web应用服务器提供的。在Spri......
  • 学习方法(1)
    不管起点如何,每个人的进步与成就的差异,在于学习和思考的方法。面对一项新技术的时候,我们怎样去学习才能循序渐进,最终理解得深刻?让我们先把可供自学的资料列出来......
  • 工具软件发现(好用的网站地址 记录)
     1、​​Mybatipse​​ 一款Eclipse插件,当编写MyBatis的关联文件的时候,用于提供内容提示和校验源码地址:​​https://github.com/mybatis/mybatipse​​安......
  • 日记网站收藏
    有的时候,突然很想写下些什么东西,祭奠一下自己那寥落的心情。有人说悲情是创作的最好灵感。不伤感,不会有用文字来记录的时间。于是到处充斥的文字都是满满的忧伤,而快乐的......
  • 常用的Java 架包(jar)的用途
    前言:如果需要在项目中引入jar包,可以采用maven,配置方式在 ​​http://mvnrepository.com​​ 查询 slf4j-api简介:slf4j并不是一种具体的日志系统,而是一个用户日志系统......
  • Placemat:快速生成占位图片器
    快速的生成一张指定大小的图片最简单的用法就是使用以下三个网址:​​https://placem.at/people​​​https://placem.at/placeshttps://placem.at/things将它当作图片链结使......
  • Activiti 学习资料收集
    ​​Activiti工作流引擎使用​​ http://www.open-open.com/lib/view/open1350460225367.html ​​Activiti初学者教程​​ javascript:void(0) ​​基于Snaker的三种流......
  • 在windows 环境下对于 git 服务器的安装和使用
    前言:虽然说在团队开发的时候会有版本控制服务器,但是个人自己开发的时候,有的时候也需要有个版本控制下,比如,你改好了一个小的功能,然后在这个功能上继续扩展,结果扩展不成......
  • 我只是在收集资料3
    Weblogic知识要点之web服务器比较http://bbs.landingbj.com/showtopic_tree.jsp?boardcode=WLTL&hit=2&showid=246914&rootid=246914#rd&sukey=4093a841665b25f20cb732192f......