Git Product home page Git Product logo

learn-nginx's Introduction

Netlify Status

学习 Nginx 配置, 包括: 编译安装、反向代理、重定向、重写、缓存、跨域配置等。

起因

起源是我的博客,博客从 IIS 伪静态起,我就一直需要接触一些配置,后来遇到了 Nginx ,被她『美丽动人的简约』打动,至些博客开始了各种折腾之路,比如:负载均衡、反向代理、前置缓存等,我把这些操作都记录到了 GitHub ,只是闲时写点笔记,也不成体系。

直到 VuePress 这么简约的文档起步,我意识到可以重新构建下站点了,起名叫:Nginx 入门教程。

基于 VuePress 构建在 GitHub Pages 中,PR 时基于 Netlify 自动部署预览测试环境方便回归。

欢迎你的加入

但我的个人能力有限,水平也有限,只能大概的构建出我的思路,希望有想法的朋友提交代码,一起完善她!阅读 贡献指南 查看更多开发者帮助。

LICENSE

MIT

learn-nginx's People

Contributors

dependabot[bot] avatar xuexb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

learn-nginx's Issues

proxy_pass

proxy_pass.md 中 94 行

// 访问:   /api/                           后端:   /v1
// 访问:   /api/xx                         后端:   /v1xx
// 访问:   /api/xx?aa                      后端:   /v1xx
// 访问:   /api-xx?aa                      后端:   /-xx?aa
location /api/ {
    proxy_pass http://node:8080/v1;
}

其中的访问 /api-xx?aa,应该是匹配失败的,不会被转发给后端。

请问如何正确的配置 nginx 反向代理(?

Nginx & Reserve Proxy

文档我没有看懂,我本地 server http://127.0.0.1:3000, 请问如何正确的配置,才能访问 back-end server http://10.1.5.203/api/test.json ?



#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       80;
        server_name  http://10.1.5.203;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		
        location / {
            root   html;
            index  index.html index.htm;
        }

        #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;
        #}
		# 202
		location /api/{
            proxy_pass   http://127.0.0.1:3000;
        }
		location / {
			add_header 'Access-Control-Allow-Origin' '*';
		}

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


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

}

Q:

  1. 我的 .conf 哪里写错了呀?

  2. Nginx 是要安装在服务器上,还是安装到本地的 PC 上?


A:

  1. proxy_pass 是指要代理的 backen IP.

  2. Nginx 安装到本地的 PC 上.

请教一个配置nginx 配置问题

目前主机在内网环境没有域名,主机上使用端口运行服务,一个javaweb网站,
由于使用多个服务,希望能将端口反代成二级目录
使用proxy_pass 代理后出现web服务的静态资源无法访问,查看浏览器发现静态资源端口号没有反代
nginx 配置如下
location /wx/ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Prefix /wx;
proxy_pass http://127.0.0.1:8080;
}
望解答!

我这个需求nginx要怎么配置

目录下有两个文件
/www/sample1 // 没有后缀的PHP文件
/www/sample2.php // 有后缀的PHP文件

想要实现访问下面的链接,PHP能正常解析执行
http://localhost/sample1 // 解析执行/www/sample1
http://localhost/sample2 // 解析执行/www/sample2
http://localhost/sample2.php // 解析执行/www/sample2

同时浏览器访问 http://localhost 可以正常目录浏览

我折腾了几个小时nginx配置,前面两个sample1, sample2的需求可以了。
但是访问http://localhost ,nginx报404错误,没法目录浏览

当前配置:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
  worker_connections 768;
  # multi_accept on;
}

http {

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  # 开启目录浏览
  autoindex on;

  server {
    listen 80;

    server_name t;
    set $base /www;
    root $base;

    index index.html index.php;

    location / {
      try_files $uri $uri.html $uri/ $uri.php?$args /index.php?$query_string @extensionless-php;
    }

    location ~ (|\.php)$ {
      try_files $uri $uri.php =404;

      # fastcgi settings
      include fastcgi_params;
      fastcgi_pass      127.0.0.1:9000;
      fastcgi_index     index.php;
      fastcgi_buffers     8 16k;
      fastcgi_buffer_size   32k;
      fastcgi_param DOCUMENT_ROOT   $realpath_root;
      fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
      fastcgi_param PHP_ADMIN_VALUE "open_basedir=/www/:/usr/lib/php/:/tmp/";
    }

    location @extensionless-php {
        rewrite ^(.*)$ $1.php last;
    }

  }

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.