切换到目录/usr/local/nginx/sbin,/usr/local为nginx的默认安装目录
#启动
./nginx
#查看命令帮助
./nginx -h
验证配置文件状态
./nginx -t
#编辑配置文件
vim /usr/local/nginx/conf/nginx.conf
# 重新载入配置文件
./nginx -s reload
# 重启 Nginx
./nginx -s reopen
# 停止 Nginx
./nginx -s stop(quit)
下面是nginx配置文件的详解参考:
https://www.cnblogs.com/liang-wei/p/5849771.html
https://www.cnblogs.com/xuey/p/7631690.html
https://blog.csdn.net/jackliu16/article/details/79444327
#user  nobody;

#nginx进程数,建议设置为等于CPU总核心数
worker_processes ;

#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#进程文件
#pid logs/nginx.pid; #工作模式与连接数上限
events {
#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
   #use epoll;
#单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections ;
} #设定http服务器
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 ; #==客户端链接超时时间
keepalive_timeout ;

#开启gzip压缩功能
#gzip on; #当配置多个server节点时,默认server names的缓存区大小就不够了,需要手动设置大一点
server_names_hash_bucket_size ; #server表示虚拟主机可以理解为一个站点,可以配置多个server节点搭建多个站点
#每一个请求进来确定使用哪个server由server_name确定
server {
#监听端口
listen ;
#域名可以有多个,用空格隔开
server_name localhost; #编码格式,避免url参数乱码
charset utf-; #access_log logs/host.access.log main; #location用来匹配同一域名下多个URI的访问规则
#比如动态资源如何跳转,静态资源如何跳转等
#location后面跟着的/代表匹配规则
location / {
#站点根目录,可以是相对路径,也可以使绝对路径
root html;
#默认主页
index index.html index.htm; #转发后端站点地址,一般用于做软负载,轮询后端服务器
#proxy_pass http://10.11.12.237:8080; #拒绝请求,返回403,一般用于某些目录禁止访问
#deny all; #允许请求
#allow all; add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
#重新定义或者添加发往后端服务器的请求头
#给请求头中添加客户请求主机名
proxy_set_header Host $host;
#给请求头中添加客户端IP
proxy_set_header X-Real-IP $remote_addr;
#将$remote_addr变量值添加在客户端“X-Forwarded-For”请求头的后面,并以逗号分隔。 如果客户端请求未携带“X-Forwarded-For”请求头,$proxy_add_x_forwarded_for变量值将与$remote_addr变量相同
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#给请求头中添加客户端的Cookie
proxy_set_header Cookie $http_cookie;
#将使用代理服务器的主域名和端口号来替换。如果端口是80,可以不加。
proxy_redirect off; #浏览器对 Cookie 有很多限制,如果 Cookie 的 Domain 部分与当前页面的 Domain 不匹配就无法写入。
#所以如果请求 A 域名,服务器 proxy_pass 到 B 域名,然后 B 服务器输出 Domian=B 的 Cookie,
#前端的页面依然停留在 A 域名上,于是浏览器就无法将 Cookie 写入。    #不仅是域名,浏览器对 Path 也有限制。我们经常会 proxy_pass 到目标服务器的某个 Path 下,
#不把这个 Path 暴露给浏览器。这时候如果目标服务器的 Cookie 写死了 Path 也会出现 Cookie 无法写入的问题。 #设置“Set-Cookie”响应头中的domain属性的替换文本,其值可以为一个字符串、正则表达式的模式或一个引用的变量
#转发后端服务器如果需要Cookie则需要将cookie domain也进行转换,否则前端域名与后端域名不一致cookie就会无法存取
       #配置规则:proxy_cookie_domain serverDomain(后端服务器域) nginxDomain(nginx服务器域)
proxy_cookie_domain localhost .testcaigou800.com; #取消当前配置级别的所有proxy_cookie_domain指令
#proxy_cookie_domain off;
#与后端服务器建立连接的超时时间。一般不可能大于75秒;
proxy_connect_timeout ;
} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
} }   #当需要对同一端口监听多个域名时,使用如下配置,端口相同域名不同,server_name也可以使用正则进行配置
  #但要注意server过多需要手动扩大server_names_hash_bucket_size缓存区大小
  server {
    listen ;
    server_name www.abc.com;
    charset utf-;
    location / {
      proxy_pass http://localhost:10001;
    }
  }
  server {
    listen ;
    server_name aaa.abc.com;
    charset utf-;
    location / {
      proxy_pass http://localhost:20002;
    }
  }
}

我们项目只用了方向代理的功能,使用实例:

#user  nobody;
worker_processes ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 2000m;
#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 ;
keepalive_timeout ;
fastcgi_connect_timeout ;
fastcgi_send_timeout ;
fastcgi_read_timeout ; proxy_connect_timeout ;
proxy_send_timeout ;
proxy_read_timeout ; #gzip on; server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen ;
# listen somename:;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# };
#} upstream tomcate_server{
server 10.xx.xx.248:;
} server {
listen ;
server_name localhost; location /api/sr_firewall {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:8005;
}
location /user_manager {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:8010;
}
location /sr_network/v2 {
proxy_redirect off;
proxy_pass http://127.0.0.1:8004;
} location /sr_sys {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:8010;
} location /sr_dev_manager {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:8010;
} location /sr_public {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:8010;
} location /sr_fire {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:8010;
} location /sr_device_automation {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:8010;
} location /sr_script {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:8010;
} location /sr_network {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:8010;
} location /sr_f5_manage {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:8007;
} location /api/sr_f5_manager {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:8007;
} location /sr_ip {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:18012;
} location /sr_data_collection {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:8011;
} location /api/v1/sunrun/data_collection {
proxy_redirect off;
proxy_pass http://10.xx.xx.248:8011;
} location / {
proxy_pass http://tomcate_server;
} location /.(html|js|css|png|gif|scss)$ {
root E:\auto-apache-tomcat-8.5.\webapps\sr_centre;
}
} # HTTPS server
#
#server {
# listen ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }

 proxy_redirect的作用和配置方法:

https://bluedest.iteye.com/blog/740302

 

linux环境下Nginx的配置及使用的更多相关文章

  1. 【环境配置】Linux环境下下载、配置java环境、安装eclipse、建立eclipse快捷方式详解

    一.首先是下载Java JDK 到目前为止的最新版本为(jdk1.8.0_60),有两种方式进行下载: 1.使用shell来进行下载,可使用如下命令直接进行下载: wget --no-check-ce ...

  2. Windows环境和Linux环境下Redis主从复制配置

    Windows环境下和Linux环境下配置Redis主从复制基本上一样,都是更改配置文件.Windows环境下修改的配置文件是:redis.windows.conf.redis.windows-ser ...

  3. linux环境下Nginx的安装

    因为工作环境大多数都是windows server服务器,仅有的linux服务器同事们都在抢着用,所以特意买了一台阿里云服务器,感兴趣的小伙伴可以了解一下,一年只要293: https://promo ...

  4. 在Linux环境下安装和配置phpmyadmin

    phpmyadmin是一种MySQL的图形化管理工具,该工具允许你在web界面上管理你的mysql数据库,不可谓不方便快捷. 此次安装与配置是在centos 6.4系统下,该系统已部署lnmp环境.关 ...

  5. Linux环境下Nginx及负载均衡

    Nginx 简介 Nginx 是一个高性能的 HTTP 和反向代理 Web 服务器,同时也提供了 IMAP/POP3/SMTP 服务.前向代理作为客户端的代理,服务端只知道代理的 IP 地址而不知道客 ...

  6. Linux环境下安装、配置Redis

    linux下安装redis 官网下载链接:https://redis.io/download 安装 下载redis压缩包 1.选择Stable(5.0)下的Download 5.0.0 链接进行下载 ...

  7. Linux环境下Redis安装配置步骤[转]

    在LInux下安装Redis的步骤如下: 1.首先下载一个Redis安装包,官网下载地址为:https://redis.io/ 2.在Linux下解压redis: tar -zxvf redis-2. ...

  8. Linux环境下Nginx配置安装PHP

    下边的安装配置方法,我试了一晚上没有成功,可能因为我的系统环境比较复杂,所以建议: 先安装PHP.使用yum命令安装,在安装配置MySQL,具体做法看博客中其他文章,至于Nginx服务器可以安装完这两 ...

  9. linux环境下nginx配置

    1.反向代理配置 #  nginx/conf/nginx.conf

随机推荐

  1. 《Java 8 in Action》Chapter 5:使用流

    流让你从外部迭代转向内部迭代,for循环显示迭代不用再写了,流内部管理对集合数据的迭代.这种处理数据的方式很有用,因为你让Stream API管理如何处理数据.这样Stream API就可以在背后进行 ...

  2. jvm系列(四):jvm调优-命令篇

    运用jvm自带的命令可以方便的在生产监控和打印堆栈的日志信息帮忙我们来定位问题!虽然jvm调优成熟的工具已经有很多:jconsole.大名鼎鼎的VisualVM,IBM的Memory Analyzer ...

  3. Leetcode之二分法专题-162. 寻找峰值(Find Peak Element)

    Leetcode之二分法专题-162. 寻找峰值(Find Peak Element) 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1] ...

  4. POJ 1077 Eight (BFS+康托展开)详解

    本题知识点和基本代码来自<算法竞赛 入门到进阶>(作者:罗勇军 郭卫斌) 如有问题欢迎巨巨们提出 题意:八数码问题是在一个3*3的棋盘上放置编号为1~8的方块,其中有一块为控制,与空格相邻 ...

  5. 洛谷 P4206 [NOI2005]聪聪与可可 题解

    题面 输入 数据的第1行为两个整数N和E,以空格分隔,分别表示森林中的景点数和连接相邻景点的路的条数. 第2行包含两个整数C和M,以空格分隔,分别表示初始时聪聪和可可所在的景点的编号. 接下来E行,每 ...

  6. cesium页面小控件的隐藏

    cesium页面小控件的隐藏 1   创建一个Viewer var viewer = new Cesium.Viewer('cesiumContainer');//cesiumContainer为di ...

  7. 树莓派的Respbian或者ubuntu系统下安装opencv最有效的方法

    第一种方法当然首选和其他安装包相同的方法pip install opencv-python安装失败后果断选择第二种方法,这第二种方法尝试过很多次了,屡试不爽 第二种方法:sudo apt-get in ...

  8. 第10讲-Java集合框架

    第10讲 Java集合框架 1.知识点 1.1.课程回顾 1.2.本章重点 1.2.1 List 1.2.2 Set 1.2.3 Map 2.具体内容 2.1.Java集合框架 2.1.1 为什么需要 ...

  9. nvm 管理多个活动的node.js版本

    前序:最近在使用taro框架开发小程序,因为安装taro时遇到一些问题,后来重新安装了node版本——v10.16.3,却影响了我本地开发的项目,故此使用nvm来管理node的版本,更加灵活的切换以支 ...

  10. 阿里巴巴资深技术专家雷卷:值得开发者关注的 Java 8 后时代的语言特性

    作者 | 阿里巴巴资深技术专家  雷卷,GitHub ID @linux-china 导读:在 Python.JavaScript 等一众编程语言崛起风靡之际,一代霸主 Java 风采虽不及当年,但仍 ...